Alihamra
Alihamra

Reputation: 484

Multiple arrays using same 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

Upvotes: 0

Views: 755

Answers (1)

user3702775
user3702775

Reputation: 271

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

Upvotes: 3

Related Questions