How To Cycle Through possible POST

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
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

How To Cycle Through possible POST

Post 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!
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post 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
}
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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()
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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:
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Post 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!
suyeic
Forum Newbie
Posts: 6
Joined: Wed Jun 13, 2007 9:22 pm
Location: Bejing China

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
Post Reply