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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.