mdnghtblue Posted July 28, 2007 Share Posted July 28, 2007 I have a form with a list of items on a "market", and users can choose which and how many items to buy by filling in any of the fields and then clicking the submit button. Every input field is coded like this: <input type="text" name="buy[<?=$item[id]?>]" size="6" value="0"> In the form handler function, I go through each value in buy[]: $i = 0; foreach($buy as $val) { print "amount $i = $buy[$i]"; // debug if($buy[$i] != 0) buyItems($i, $buy[$i]); $i++; } The problem is that it doesn't go through every value, it stops after about 20 array values or less. (the first time I noticed this, it only went through about 20 array spots, right now it's at about 12, it keeps going down) I checked to see if those other array values are just not going through the foreach loop, but those array spots don't exist. How come those fields aren't being put into the array? Quote Link to comment https://forums.phpfreaks.com/topic/62248-solved-array-problem/ Share on other sites More sharing options...
Barand Posted July 28, 2007 Share Posted July 28, 2007 <?php foreach ($_POST['buy'] as $id => $qty) // or $_GET if form method is get { echo "item $id : $qty<br/>"; if ($qty != 0) buyItems($id, $qty); } Quote Link to comment https://forums.phpfreaks.com/topic/62248-solved-array-problem/#findComment-309808 Share on other sites More sharing options...
mdnghtblue Posted July 28, 2007 Author Share Posted July 28, 2007 Amazing.... can you explain what was wrong before? I just didn't call the loop the right way? Quote Link to comment https://forums.phpfreaks.com/topic/62248-solved-array-problem/#findComment-309812 Share on other sites More sharing options...
Barand Posted July 28, 2007 Share Posted July 28, 2007 you were assuming that $i would always equal the posted item id Quote Link to comment https://forums.phpfreaks.com/topic/62248-solved-array-problem/#findComment-309813 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.