PHP通过表单传递和接收二维数组 [英] PHP pass and receive two dimensional array from form

查看:424
本文介绍了PHP通过表单传递和接收二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将两个成对的变量,一个类别及其ID(用于多个用户创建的类别)从Web表单传递到文件以进行处理.我想让它们保持链接,以便如果有人更改名称字段,我们很清楚他们正在更改什么记录,因为该记录已通过ID链接到其他表.

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.

最初尝试使用二维数组...已经放弃了这种方法.现在,按照下面的建议尝试使用两个一维数组.我要使用数组,因为表单上的条目数取决于用户创建的类别数.类别表具有id(int),cat(text)和userid(int)字段

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

传递两个数组的困难之处在于如何链接两个数组.如果我将类别作为数组传递给接收页面,则可以遍历值以生成sql语句来更改每个条目.但是,我不知道如何获取标识记录所需的正确ID.如果我在foreach(catsarray作为$ val)中使用foreach($ idarray作为$ id),我将为每只猫获得多个ID.如何与遍历猫同步地遍历id?非常感谢您的帮助,因为我现在花了两天时间:(

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);
}

推荐答案

用于创建二维数组的良好且单一的语法;

good and single syntax for creating two dimensional array;

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

接收猫时;

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

更多示例;

根据此定义,

<input type="text" name="cat[$cat][$id]" value="cat1">

foreach ($_POST['cat'] as $a=>$b){
    // $a == $cat
    foreach($b as $c=>$d) {
       // $c == $id
    }
}

这篇关于PHP通过表单传递和接收二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆