Jump to content

Foreach loop


Schlo_50

Recommended Posts

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!

 

Link to comment
https://forums.phpfreaks.com/topic/93501-foreach-loop/
Share on other sites

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

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/93501-foreach-loop/#findComment-479055
Share on other sites

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!

Link to comment
https://forums.phpfreaks.com/topic/93501-foreach-loop/#findComment-479121
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/93501-foreach-loop/#findComment-479429
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.