好的,我正在为我的雇主构建一个输入产品的系统,他们有非常具体的要求。我有一个表单,其中包含动态生成的字段,如下所示... (显然不是完全相同的代码示例,但概念相同)
<input type="text" name="attribute[20]"> inputted value = height
<input type="text" name="attribute[27]"> inputted value = width
这些数字是基于数据库中的内容生成的,例如20对应“宽度”,27对应“高度”。
因此,一旦用户输入了这些值,我就需要将这些值存入数据库...或在测试中输出。
foreach ($_POST['attribute'] as $attributes){
echo key($attributes).' '.$attributes.'<br>';
}
那么输出应该是...
20 height value<br>
27 width value
但实际输出的是...
height value<br>
width value
发生了什么?我有类似的问题...但是定义的数字可以有多个输入....这个也完美地解决了。
<input type="text" name="option[][20]"> inputted value = option 1
<input type="text" name="option[][20]"> inputted value = option 2
<input type="text" name="option[][27]"> inputted value = option 1
foreach ($_POST['option'] as $options){
echo key($options).' ';
foreach ($options as $option){
echo $option.'<br>';
}
这段代码可以完美输出...
20 option 1<br>
20 option 2<br>
27 option 1
我不明白为什么更复杂的代码可以工作,而更简单的却不能。我知道我的编码方法有点与众不同,但这就是它的样子,哈哈。非常感谢任何帮助。
编辑:按要求进行变量转储
数组(22) { ["pID"]=> string(12) "test product" ["pPrice"]=> string(0) "" ["pName"]=> string(0) "" ["pRRP"]=> string(0) "" ["pPostSize"]=> string(0) "" ["pOurPrice"]=> string(0) "" ["pEstDelivery"]=> string(0) "" ["pWeight"]=> string(0) "" ["pEAN"]=> string(0) "" ["pOrder"]=> string(0) "" ["pStock"]=> string(0) "" ["pManufacturer"]=> string(0) "" ["pType"]=> string(13) "Shower Valves" ["pRange"]=> string(0) "" ["cat"]=> array(2) { [0]=> string(2) "72" [1]=> string(2) "23" } ["attribute"]=> array(2) { [0]=> string(5) "width" [1]=> string(6) "height" } ["option"]=> array(3) { [0]=> array(1) { [11]=> string(6) "works1" } [1]=> array(1) { [10]=> string(6) "works1" } [2]=> array(1) { [10]=> string(6) "works2" } } ["pLongdescription"]=> string(0) "" ["meta_description"]=> string(0) "" ["meta_keyword"]=> string(0) "" ["meta_title"]=> string(0) "" ["action"]=> string(6) "create" }
粗体部分是第二个例子中成功输出的部分。但是,斜体加粗部分返回0,而实际上表单名称值为20。