【发布时间】:2014-08-29 02:36:50
【问题描述】:
让我们从表格布局开始,如下所示:
Item No. [Store 1] [Store 2] [Store 3]
[ x1 ] [ 3 ] [ 2 ] [ 5 ]
[ x2 ] [ 0 ] [ 5 ] [ 1 ]
请注意此[ ] 表示为输入文本字段。
现在每个文本字段都有一个名称设置为array:
$n= array();
$item = array();
$s = array();
货品编号
echo "<input type='text' value='".$row['item_no']."' name='item[]' readonly='readonly' />";
存储 1 ,2 和 3
echo "<input type='text' value='".$row['short']."' name='s[]' readonly='readonly' />";
每个商店下方的字段
echo "<input type='text' value='' name='n[]' />";
我希望它清楚,如您所见,找到了 3 个arrays。现在,当我将此表单提交到下一页时...
if(isset($_POST['n']) && isset($_POST['s']) && isset($_POST['item'])){
if(is_array($_POST['n']) && is_array($_POST['s']) && is_array($_POST['item'])) {
foreach( $_POST['n'] as $index => $nqty) {
echo 'QTY = '.$nqty . ' STORE = ' . $_POST['s'][$index]."<br />";
//End of Foreach loop
}
}}
上面代码的输出如下所示:
QTY = 3 STORE = store 1
QTY = 2 STORE = store 2
QTY = 5 STORE = store 3
QTY = 0 STORE = store 1
QTY = 5 STORE = store 2
QTY = 1 STORE = store 3
我现在想要的只是我应该如何或在哪里放置项目编号。上面代码中的数组..换句话说,我希望输出完全像这样:
QTY = 3 STORE = store 1 Item No. = x1
QTY = 2 STORE = store 2 Item No. = x1
QTY = 5 STORE = store 3 Item No. = x1
QTY = 0 STORE = store 1 Item No. = x2
QTY = 5 STORE = store 2 Item No. = x2
QTY = 1 STORE = store 3 Item No. = x2
有可能吗?如果不是,那么另一种方法是什么。
请帮忙,我完全被这个项目卡住了。
回复@user3702775
我已经测试了您的代码,更改了 s[][] 和 n[][] 的名称,其中输出如下所示:
QTY = 3 STORE = store 1 Item No. - x1
QTY = 2 STORE = store 2 Item No. - x2
它甚至错过了商店 3 和下一行。
存储 1 ,2 和 3
echo "<input type='text' value='".$row['short']."' name='s[][]' readonly='readonly' />";
每个商店下方的字段
echo "<input type='text' value='' name='n[][]' />";
foreach 代码:
foreach( $_POST['item'] as $key=>$value){
foreach( $_POST['n'][$key] as $index => $nqty) {
echo 'QTY = '.$nqty . ' STORE = ' . $_POST['s'][$key][$index]." Item No. - ".$value."<Br />";
//End of Foreach loop
}
}
已解决:
更改以下输入文本字段的名称,如下所示:
货品编号
echo "<input type='text' value='".$row['item_no']."' name='item[$j]' readonly='readonly' />";
存储 1 ,2 和 3
echo "<input type='text' value='".$row['short']."' name='s[$j][]' readonly='readonly' />";
每个商店下方的字段
echo "<input type='text' value='' name='n[$j][]' />";
然后在代码开头赋值$j=0;
并把$j++;放在最后
然后在提交页面:
foreach( $_POST['item'] as $key=>$value){
foreach( $_POST['n'][$key] as $index => $nqty) {
echo 'QTY = '.$nqty . ' STORE = ' . $_POST['s'][$key][$index]." Item No. - ".$value."<Br />";
//End of Foreach loop
}
}
感谢@user3702775
【问题讨论】: