Before I spend a lot of time on this I want to get some input on possible ways to handle this...
I have a client who is using a third part shopping cart on their site. At the end of every transaction, there is a silent post back to the server where the site is hosted that allows me to capture the order #, address info, etc. Everything is clearly separated with an ampersand, BUT for each item someone has purchased, rather than giving you an array (...item[]=gold%20watch&item[]=belt&20buckle...) they have already assigned variables of item1=, item2=, etc.
So my question is, rather than having a bunch of $_POST variables predefined, is there a way to break these out into an array so I can run a loop so that no matter how many items someone buys, I don't have to create a huge number of table fields in the database to accomodate this?
-PM
convert URL string to array
Moderator: General Moderators
- Gente
- Forum Contributor
- Posts: 252
- Joined: Wed Jun 13, 2007 9:43 am
- Location: Ukraine, Kharkov
- Contact:
Try this example from PHP Manual
Code: Select all
<?php
$a = explode('&', $_SERVER['QUERY_STRING']);
$i = 0;
while ($i < count($a)) {
$b = split('=', $a[$i]);
echo 'Value for parameter ', htmlspecialchars(urldecode($b[0])),
' is ', htmlspecialchars(urldecode($b[1])), "<br />\n";
$i++;
}
?>- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
split() is a no, no. explode() or preg_split().
Well.. you can foreach() $_POST, or use parse_str(), or get creative with preg_replace() and parse_str().
Well.. you can foreach() $_POST, or use parse_str(), or get creative with preg_replace() and parse_str().
Well, all I had to do was perform a str_replace on the $key variable generated by the while loop, and then determine if it's an item via if/else statement. If it is an item, it will insert the items into a separate table and if not, I'll put it in a larger query further down in the script (haven't written that yet):
Code: Select all
$ar=$_GET;
while (list($key,$val) = each($ar))
{
$newKey = substr_replace($key, '', 4);
if($newKey=="item") { $key = "item";
//if this is an item I'm going to write it to a separate table anyway so do the query...
mysql_select_db($mysql);
$item_query = "INSERT INTO order_items ('','$_GET[order_num]','$val')";
mysql_query($item_query) or die('Error, insert query failed');
echo '<p>',$item_query;
}
else { echo "$key=$val<br>"; }
}