doy2255 2014-07-08 11:20
浏览 34
已采纳

使用相同foreach的多个数组

Let's start with the table layout as follows :

Item No.          [Store 1]      [Store 2]    [Store 3]
[   x1   ]         [  3   ]       [  2   ]     [  5   ]
[   x2   ]         [  0   ]       [  5   ]     [  1   ]

Please Note this [ ] Indicates as input text field.

Now each text field has a name set as an array :

$n= array();
$item = array();
$s = array();

Item No.

echo "<input type='text' value='".$row['item_no']."' name='item[]' readonly='readonly' />";

Store 1 ,2 and 3

echo "<input type='text' value='".$row['short']."' name='s[]' readonly='readonly' />";

Fields below Each Store

echo "<input type='text' value='' name='n[]' />";

I hope its clear, as you can see there are 3 arrays found. Now when I submit this form to the next page...

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
}

}}

The output for the code above is shown below :

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

All I want now is that how or where should I place the item no. array in the above code .. In other words I want the output to be exactly like this :

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

Is it possible? If not what's the other way of doing it.

Please help, I'm totally stuck at this project.




Reply for @user3702775

I've tested your code, changed the name for s[][] and n[][] where the output shown as follows :

QTY = 3 STORE = store 1 Item No. - x1
QTY = 2 STORE = store 2 Item No. - x2 

Its even missing the store 3 and the next row.

Store 1 ,2 and 3

echo "<input type='text' value='".$row['short']."' name='s[][]' readonly='readonly' />";

Fields below Each Store

echo "<input type='text' value='' name='n[][]' />";

foreach code :

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



Solved :

Change the name for following input text fields as follows :

Item No.

echo "<input type='text' value='".$row['item_no']."' name='item[$j]' readonly='readonly' />";

Store 1 ,2 and 3

echo "<input type='text' value='".$row['short']."' name='s[$j][]' readonly='readonly' />";

Fields below Each Store

echo "<input type='text' value='' name='n[$j][]' />";

Then Assign $j=0; at the begining of the code

and place $j++; at the end

Then at the submit page :

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
  }

}

Thanks for @user3702775

  • 写回答

1条回答 默认 最新

  • download2565 2014-07-08 11:35
    关注

    Make your store and fields array as s[][] and n[][] and set the first index for each item.

    E.g. - For first item the store and fields name will be s[0][] and n[0][] and so on.

    Then after submitting your loop will be something like -

    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
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置