Handling $_POST vars in a foreach loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Bubble_Nut
Forum Newbie
Posts: 2
Joined: Wed Nov 17, 2010 11:51 pm

Handling $_POST vars in a foreach loop

Post by Bubble_Nut »

Hi there

this is my first post so I hope I am doing this in the correct place. I need a little help here, I have looked at my books and the web forums for some answer but to no avail.

Simply, I am passing normal post data to a page to handle the data. The form is created dynamically and as such I do not know the whick field names will be there.

The two main values I need to capture are, for instance qty_235 and unitprice_235, where the 235 is the product id. Using the standard

Code: Select all

foreach ($_POST as $key => $value) {
...
}

means I can list all the values, however, in each case I want intervene in the loop and retrieve the product id [235 in the case above], the unitprice and qty.

I have tried inserting if statement to strrpos for q or u, explode the qty to retrieve the pid, pretty much everything I can think of but by inserting the if statement the loop breaks and I end up the last value sent from the form only?

Thanks in advance for any help

A
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: Handling $_POST vars in a foreach loop

Post by s992 »

This may not be an option and is not a direct answer to your question, but rather than generating the fields dynamically, you could pass a hidden value that contains the product ID when submitting the form.

For example,

Code: Select all

<input type="hidden" name="product_id" value="<?php echo $id; ?>" />
That way, you could standardize your form field names(qty instead of qty_235, etc) and still have access to the product ID without having to do anything fancy. Obviously, you'll need to replace the $id variable with whatever variable holds the product ID on the form's page.
Bubble_Nut
Forum Newbie
Posts: 2
Joined: Wed Nov 17, 2010 11:51 pm

Re: Handling $_POST vars in a foreach loop

Post by Bubble_Nut »

Thanks s992, in the first place I did just what you suggested but this only gave me another var for each set of data, I need to split up the

$key =qty_[pid]
$value = [integer]

$key=unitprice_[pid]
$value=[integer]

so I will have the pid present in any case, at the minute I have got all the data being fed into a string and this is the result of one order

unitprice_8-0.66-qty_8-1-unitprice_9-0.84-qty_9-2-unitprice_10-1.60-qty_10-0-unitprice_12-1.20-qty_12-0-unitprice_15-1.20-qty_15-0-unitprice_20-1.41-qty_20-0-unitprice_21-1.50-qty_21-0-unitprice_68-1.22-qty_68-0-unitprice_69-1.35-qty_69-0-submit-Send Order-

I need to be to in the foreach loop separated the values pid, unitprice and qty, do the calulation by and pull the relevant data from the MyQSQL DB table to create a confirmation page with all relevant details on it.

I have built many ecom sites but never had this issue before

Thanks for any help offered, A
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Handling $_POST vars in a foreach loop

Post by Christopher »

You might want to pass the values as arrays. That would make it easier to both set the values in the form and get them out of $_POST:

Code: Select all

<input type="hidden" name="qty[235]" value="<?php echo $qty[235]; ?>" />
<input type="hidden" name="unitprice[235]" value="<?php echo $unitprice[235]; ?>" />;
(#10850)
Post Reply