Schlo_50 Posted February 28, 2008 Share Posted February 28, 2008 Hey Guys, Just a quick one, I have some items displayed on a page. Next to each item is a checkbox and textbox for the user to select and then type the quantity of that item they desire: <input name=\"orderid[]\" value=\"{$productid}\" type=\"checkbox\"/> <input name=\"quantity[{$productid}]\" type=\"text\" size=\"3\" maxlength=\"3\" /> Later on in the script i want to use a foreach loop on the array which goes like this: foreach($quantity as $keyb => $valueb) { print 'Item# '.$keyb.' = Quantity'.$valueb.'<br>'; } This outputs the following: Item# : Quantity 17 : 18 14 : 15 16 : 20 : 12 : 9 : 15 : 21 : 23 : 22 : 13 : As you can see all of the item numbers are displayed even though the user hasn't entered any quantities in the text box next to those items. I need a little snippet of code that only displays the item number and quantities of the items checked. Can anybody help? Thanks in advance! Quote Link to comment Share on other sites More sharing options...
tinker Posted February 28, 2008 Share Posted February 28, 2008 i've not really look at this (yum, yum) but: foreach($quantity as $keyb => $valueb) { if(strcmp($valueb, "")!=0) { print 'Item# '.$keyb.' = Quantity'.$valueb.'<br>'; } } Quote Link to comment Share on other sites More sharing options...
BenInBlack Posted February 28, 2008 Share Posted February 28, 2008 ok this is wrong <input name=\"quantity[{$productid}]\" type=\"text\" size=\"3\" maxlength=\"3\" /> it should be <input name=\"productid[]\" value="{$productid}" type=\"text\" size=\"3\" maxlength=\"3\" /> <input name=\"quantity[]\" value="{$qty}" type=\"text\" size=\"3\" maxlength=\"3\" /> you can't enter a value on the html side in the brackets [] that just tells php to take the all the items named productid into a php array, so you need to sync the assignments so that the productid and qty are always paired Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted February 28, 2008 Author Share Posted February 28, 2008 Ok, Thanks for the comments guys. So if i wanted to get the information collected into my database in their own rows (item#, quantity) what would i have to do to the file i attached? (sorry you have to download the attachment, just that sometimes when posting code snippets on this site i can't because i have copied and pasted them from my editor.. Thanks guys! Quote Link to comment Share on other sites More sharing options...
BenInBlack Posted February 28, 2008 Share Posted February 28, 2008 you didn't include the code that was sending the data, you are keying off of $quantity foreach($quantity as $keyb => $valueb) { how are you passing the 2 values? ok, you may be able to get away with this by using the = <input name=\"quantity[]\" value="{$productid}={$qty}" type=\"text\" size=\"3\" maxlength=\"3\" /> But, in any case the how you are passing the values is crucial, to helping Quote Link to comment 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.