Page 1 of 1

How To Cycle Through possible POST

Posted: Thu Aug 30, 2007 2:57 pm
by seodevhead
Hey guys.. I have a very simple problem, and I'm sure the method I have devised is far from optimal, so I come to seek your advice on how you would do this...

I have an ecommerce payment gateway passing back certain parameters about a customer's order using an HTTP POST. The most important parameter of all is the 'product_id' parameter. Each product is assigned a product id such as 'shoes'.

So if a customer orders just the 'shoes' item from the store, one of the parameters passed back to my script is $_POST['product_id'] = 'shoes'.

If a customer orders 'shoes' and a 'hat', then a second product id parameter is posted as 'product_id1' which will equal, of course, 'hat'.

If more items are ordered than two, then there is a product id for each product passed back, and posted variable is incremented like so:

product_id
product_id1
product_id2
product_id3
...and so on.


Problem is I have no idea how many items each customer ordered, so I need a loop to keep checking how many "product_id(X)" variables are present and be able to store each product_id in an array so I know what was ordered.

What is the best way to do this? For the life of me I can't seem to feel confident with anything I've setup. It works, but I'm trying to become a better programmer and not just a hacksaw developer.

Thanks for any help!

Posted: Thu Aug 30, 2007 3:19 pm
by TheMoose
I'd have to think about it a bit more in depth, but if you want to solve your current problem you could do something like:

Code: Select all

$i = 0;
while(isset($_POST['product'.$i])) {
// stuff here
}

Posted: Thu Aug 30, 2007 3:20 pm
by Zoxive
I would probably do something along the lines of..

Code: Select all

function makeProductArray($Input){
	$Products=array();
	foreach($Input as $name=>$data){
		if(strstr('product_id',$name)){
			$Products[]=$data;	
		}
	}
	return $Products;
}
//
$Products = makeProductArray($_POST);
And i used strstr instead of regex mainly because its faster, but of course you could always use regex too.

Posted: Thu Aug 30, 2007 3:22 pm
by John Cartwright
Why not use an array instead?

Code: Select all

<input type="product[1]" .. />  
<input type="product[14]" .. />
<input type="product[21]" .. />
<input type="product[29]" .. /> 
Then it's simple to use a foreach()

Posted: Thu Aug 30, 2007 3:28 pm
by Zoxive
Jcart wrote:Why not use an array instead?

Code: Select all

<input type="product[1]" .. />  
<input type="product[14]" .. />
<input type="product[21]" .. />
<input type="product[29]" .. /> 
Then it's simple to use a foreach()
seodevhead wrote:I have an ecommerce payment gateway passing back certain parameters about a customer's order using an HTTP POST.

Posted: Thu Aug 30, 2007 3:56 pm
by John Cartwright
Zoxive wrote:
Jcart wrote:Why not use an array instead?

Code: Select all

<input type="product[1]" .. />  
<input type="product[14]" .. />
<input type="product[21]" .. />
<input type="product[29]" .. /> 
Then it's simple to use a foreach()
seodevhead wrote:I have an ecommerce payment gateway passing back certain parameters about a customer's order using an HTTP POST.
:oops:

Posted: Thu Aug 30, 2007 10:45 pm
by seodevhead
TheMoose wrote:

Code: Select all

$i = 0;
while(isset($_POST['product'.$i])) {
// stuff here
}
I have pretty much what TheMoose suggested. Maybe it is the "right" way to do it... who knows. Perhaps there are bigger fish to fry. :)

Thanks guys!

Posted: Thu Aug 30, 2007 11:00 pm
by suyeic
why not put them in a array as a cookie or seesion . Or listed all the items in a table, use checkbox or radiobox, then post it out.

Posted: Fri Aug 31, 2007 1:21 pm
by Weirdan
Or listed all the items in a table, use checkbox or radiobox, then post it out.
seodevhead does not have any control over the format of the data he receives
why not put them in a array as a cookie or seesion
most probably payment gateway posts the data directly to his server, so no user cookie (or session for that matter) could be used.