How To Cycle Through possible POST
Moderator: General Moderators
- seodevhead
- Forum Regular
- Posts: 705
- Joined: Sat Oct 08, 2005 8:18 pm
- Location: Windermere, FL
How To Cycle Through possible POST
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!
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!
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
}I would probably do something along the lines of..
And i used strstr instead of regex mainly because its faster, but of course you could always use regex too.
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);- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Why not use an array instead?
Then it's simple to use a foreach()
Code: Select all
<input type="product[1]" .. />
<input type="product[14]" .. />
<input type="product[21]" .. />
<input type="product[29]" .. />
Jcart wrote:Why not use an array instead?
Then it's simple to use a foreach()Code: Select all
<input type="product[1]" .. /> <input type="product[14]" .. /> <input type="product[21]" .. /> <input type="product[29]" .. />
seodevhead wrote:I have an ecommerce payment gateway passing back certain parameters about a customer's order using an HTTP POST.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Zoxive wrote:Jcart wrote:Why not use an array instead?
Then it's simple to use a foreach()Code: Select all
<input type="product[1]" .. /> <input type="product[14]" .. /> <input type="product[21]" .. /> <input type="product[29]" .. />seodevhead wrote:I have an ecommerce payment gateway passing back certain parameters about a customer's order using an HTTP POST.
- seodevhead
- Forum Regular
- Posts: 705
- Joined: Sat Oct 08, 2005 8:18 pm
- Location: Windermere, FL
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.TheMoose wrote:Code: Select all
$i = 0; while(isset($_POST['product'.$i])) { // stuff here }
Thanks guys!
seodevhead does not have any control over the format of the data he receivesOr listed all the items in a table, use checkbox or radiobox, then post it out.
most probably payment gateway posts the data directly to his server, so no user cookie (or session for that matter) could be used.why not put them in a array as a cookie or seesion