knightsjoker Posted April 18, 2011 Share Posted April 18, 2011 Hi guys, Was wondering if anyone can help me with this. Here's what I'm trying to do: I've retrieved values from table a from mysql and post them on a form. echo "<tr><td>" .$row['id']. "</td><td>" .$row['name']. "<input type='hidden' name=name[] value=" .$row['name']. "></td><td><select name='item[]'><option value ='' selected='selected'>Please Select</option><option value ='1'>1</option><option value ='2'>2</option><option value ='3'>3</option></td></tr>"; and now i'm trying to display these information one more time on a different page. So I did the usual retrieval $name = $_POST['name']; $item = $_POST['item']; Then I'm using foreach loop to retrieve the item values. foreach ($item as $getitem => $value) { echo "<tr><td>"; echo $value; echo "</td></tr>"; } Here is where I'm stumped. What's the best way to associate the name to the item. while loop? Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/234013-nested-loop-need-help/ Share on other sites More sharing options...
dcro2 Posted April 18, 2011 Share Posted April 18, 2011 They'll be in a numerically-indexed array, so you can use a for() loop to go through them: for($i=0; $i<count($_POST['name']); $i++) { $name = $_POST['name'][$i]; $item = $_POST['item'][$i]; echo "$name: $item"; } Quote Link to comment https://forums.phpfreaks.com/topic/234013-nested-loop-need-help/#findComment-1202810 Share on other sites More sharing options...
codebyren Posted April 18, 2011 Share Posted April 18, 2011 They'll be in a numerically-indexed array, so you can use a for() loop to go through them... I was thinking the same thing - but if the form is manipulated in any way to submit less names than items or vice versa then the numeric indices won't match up. Not sure how serious this is in the context of the application though... Something for the OP to think about anyway. Quote Link to comment https://forums.phpfreaks.com/topic/234013-nested-loop-need-help/#findComment-1202866 Share on other sites More sharing options...
dcro2 Posted April 18, 2011 Share Posted April 18, 2011 Well, I guess if you're really paranoid... if($count($_POST['name']) != count($_POST['item'])) die("Hmm. Something's fishy here..."); Quote Link to comment https://forums.phpfreaks.com/topic/234013-nested-loop-need-help/#findComment-1202929 Share on other sites More sharing options...
knightsjoker Posted April 18, 2011 Author Share Posted April 18, 2011 I got it. I had to rewrite the table view using for loop and do the counting since the form is dynamic. and so the table view will be like: echo "<tr><td>" .$row['id']. "</td><td>" .$row['name']. "<input type='hidden' name='varname[" .$j. "][" .$i. "]' value='" .$row['name']. "'></td><td><select name='quantity[" .$j. "][" .$i. "]'><option value ='0' selected='selected'>0</option><option value ='1'>1</option><option value ='2'>2</option><option value ='3'>3</option></td></tr>"; $i++; then you do the usual array retrieval and to spew it back out... for($j=1; $j<=count($itemname); $j++) { for ($i=0; $i<count($itemname[$j]); $i++) { $getvalues1 = $itemname[$j][$i]; $getvalues2 = $itemquantity[$j][$i]; echo "<li>"; echo "<input type='text' name='itemname' value='" .$getvalues1. "' readonly='readonly'>"; echo "</li>"; echo "<li>"; echo "<input type='text' name='itemquantity' value='" .$getvalues2. "' readonly='readonly'>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/234013-nested-loop-need-help/#findComment-1202937 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.