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,从webform到文件进行处理。我想保持链接,所以如果有人更改名称字段,我们清楚他们正在更改哪条记录,因为此记录通过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
最初尝试二维阵列......已经放弃了这种方法。现在尝试按照下面的建议使用两个一维数组。我将使用数组,因为表单上的条目数取决于用户创建的类别数。 Categories表包含id(int),cat(text)和userid(int)字段
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 :(
传递两个数组的难度在于如何链接这两个数组。如果我将类别作为数组传递给接收页面,我可以迭代值来生成sql语句来更改每个条目。但是,我无法弄清楚如何获得识别记录所需的正确ID。如果我在foreach中使用foreach($ idarray作为$ id)(catsarray为$ val),我会为每只猫获得多个id。如何通过迭代猫迭代迭代id?非常感谢您的帮助,因为我现在花了两天时间:(
//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 个解决方案
#1
4
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
}
}
#1
4
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
}
}