doucheng1063 2012-04-16 20:01 采纳率: 100%
浏览 491
已采纳

PHP从表单传递并接收二维数组

I am trying to pass two paired variables, a category and its id for multiple user-created categories from a webform to a file for processing. I want to keep them linked so if someone changes the name field, we are clear what record they are changing as this record is linked to other tables by id.

Originally tried two dimensional array...have abandoned that approach. Now trying to use two one dimensional arrays as per suggestion below. I am going with arrays because number of entries on form depends on how many categories user has created. Categories table has id(int), cat(text) and userid(int) fields

Difficulty with passing two arrays is how to link the two. If I pass categories as array to receiving page, I can iterate through values to produce sql statements to change each entry. However, I can't figure out how to get correct id which is needed to identify record. If I use a foreach($idarray as $id) inside the foreach(catsarray as $val) I will get multiple ids for each cat. How do I iterate through ids in synch with iterating through cats? Many thanks for help as I have now spent two days on this :(

//I am working with $catsarray and $idarray, each with identical indexes, 1,2,3 etc.
//How do I get appropriate id for each cat
foreach($catsarray as $val) {
$sql = "UPDATE cats, set name = $val WHERE (userid ='$userid' AND id=??????)";
mysql_query($sql);
}
  • 写回答

1条回答 默认 最新

  • douling1936 2012-04-16 20:06
    关注

    good and single syntax for creating two dimensional array;

      <input type="text" name="cat[]" value="cat1">
      <input type="text" name="cat[]" value="cat2">
        ...
    

    While receiving cats;

    echo $_POST['cat'][0] // echoes cat1
    echo $_POST['cat'][1] // echoes cat2
    

    Further example;

    According to this definition,

    <input type="text" name="cat[$cat][$id]" value="cat1">
    
    foreach ($_POST['cat'] as $a=>$b){
        // $a == $cat
        foreach($b as $c=>$d) {
           // $c == $id
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog