problem storing details from shopping cart passback

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
RobOgden
Forum Newbie
Posts: 5
Joined: Wed Mar 22, 2006 2:26 am

problem storing details from shopping cart passback

Post by RobOgden »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

I have PHP version PHP 4.3.10, database is MySQL 3.23.56 and operating system is RedHat linux 7.3. 

I have a problem with a shopping cart passback sent via <input> tags to a page on our site. 

If the customer has bought more than one item, the information will come as:

Code: Select all

<input type="hidden" name="product_id1" value="2" />
then

Code: Select all

<input type="hidden" name="product_id2" value="1" />


and so on, through all the items in the shopping cart.

I want to input this information into a database table called 'orders', setting up a 'while' or 'foreach' loop to enter the quantity of each product one by one.

I have the following rough plan:

Code: Select all

while ($num = ltrim( basename( $_POST["product_id" . "")); { 
$product_id = $product_id . $num; 
$quantity = $quantity . $num; 
$query4 = "INSERT into orders (client_id,product_id,order_number,quantity) VALUES ('$client_id', '$product_id', '$order_number', '$quantity'); 
$result4 = @mysql_query ($query4); 
if ($result4) { 
$query5 = "UPDATE products SET quantity_sold = quantity_sold + $num WHERE product_id = $product_id"; 
$result5 = @mysql_query ($query5); 
mysql_close(); 
} 
}
Rather than exploding the contents of the array, I need to explode the title ('product_id#') itself to extract the relevant number from the end. I realise the 1st and 2nd lines of my code can't possibly work, but it should give you an idea of what I am trying to do I hope!!

Could you possibly advise me with this? I would be really grateful.

Yours,

Robert


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd use a foreach() against the $_POST array. Looking at each key to see if it starts with "product_id" followed by numbers (extracting them at the same time with preg_match()) then, similar to the rest of your code, create an insert query.
RobOgden
Forum Newbie
Posts: 5
Joined: Wed Mar 22, 2006 2:26 am

Post by RobOgden »

Thanks feyd. I think I'm nearly there! I actually used preg_replace. Would that be ok?

I know I'm wrong with the asterisks and the $_POST['quantity['$num']'], but need something to link the

Code: Select all

<input type="hidden" name="product_id#" value="2" />
with the

Code: Select all

<input type="hidden" name="quantity#" value="2" />
i.e. the #s must match!

Code: Select all

foreach ($_POST['product_id*']) {
$product_id = $_POST['product_id*'];
$num= preg_replace('/[^0-9]/','',$product_id);

$product_id = $_POST['product_id['$num']'];
$quantity = $_POST['quantity['$num']'];
$query4 = "INSERT into orders (client_id,product_id,order_number,quantity) VALUES ('$client_id', '$product_id', '$order_number', '$quantity');
$result4 = @mysql_query ($query4);
if ($result4) {
$query5 = "UPDATE products SET quantity_sold = quantity_sold + $quantity WHERE product_id = $product_id";
$result5 = @mysql_query ($query5);
}
}
mysql_close();

Do you know how this can be implemented? I'd be really grateful.
All the best,

Rob
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I won't do it all for you, you won't learn much then. :)

It'd start out like this:

Code: Select all

foreach($_POST as $key => $value)
{
  // check $key to see if it starts with product_id, continue if it does
  // pull out the ID and quantities.
  // insert them
}
RobOgden
Forum Newbie
Posts: 5
Joined: Wed Mar 22, 2006 2:26 am

Post by RobOgden »

Hi Feyd,

Thanks very much! It now works:

Code: Select all

foreach($_POST as $key => $value) 
{ 
$title = preg_replace('/[^a-z,A-Z]/','',$key);
$str = "$title[0]$title[1]$title[2]$title[3]$title[4]$title[5]$title[6]$title[7]$title[8]"; 
if ($str == 'productid') {
$product_id = $value;
$num = preg_replace('/[^0-9]/','',$key); 
} elseif ($str == 'quantity') {
$quantity = $value;
$query4 = "INSERT into orders (client_id, product_id, order_number, quantity, date) VALUES ('$client_id', '$product_id', '$order_number', '$quantity', NOW())";
$result4 = @mysql_query ($query4);

if ($result4) {
$query5 = "UPDATE products SET quantity_sold = quantity_sold + '$quantity' WHERE product_id = $product_id";
$result5 = @mysql_query ($query5);
}
}
}
All the best,

Robert
RobOgden
Forum Newbie
Posts: 5
Joined: Wed Mar 22, 2006 2:26 am

Post by RobOgden »

Hi Feyd,

So nearly works, but if the input parameters are sent in reverse order it messes up, and can't match

Code: Select all

<input type="hidden" name="product_id2" value="1" />
with

Code: Select all

<input type="hidden" name="quantity2" value="3" />
I think I'm at the limits of my abilities now! I have:

Code: Select all

foreach($_POST as $key => $value) 
{ 
$title = preg_replace('/[^a-z,A-Z]/','',$key);
$num = preg_replace('/[^0-9]/','',$key); 
$str = "$title[0]$title[1]$title[2]$title[3]$title[4]$title[5]$title[6]$title[7]$title[8]"; 
if ($str == 'productid') {
$product_id = $value;
} elseif ($str == 'quantity') {
$quantity = $value;
$query4 = "INSERT into orders (client_id, product_id, order_number, quantity, date) VALUES ('$client_id', '$product_id', '$order_number', '$quantity', NOW())";
$result4 = @mysql_query ($query4);

if ($result4) {
$query5 = "UPDATE products SET quantity_sold = quantity_sold + '$quantity' WHERE product_id = $product_id";
$result5 = @mysql_query ($query5);
}
}

}
I need to match up the

Code: Select all

$num
variable, which is currently not being used!

Any ideas!!

Robert
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

stick them into an array to act as a collection pool. After processing all the fields submitted, you can iterate over the array creating insertions.
RobOgden
Forum Newbie
Posts: 5
Joined: Wed Mar 22, 2006 2:26 am

Post by RobOgden »

Hi Feyd. It's fixed! Thanks for your help. I now have:

Code: Select all

$items = array();
    foreach($_POST as $k => $value) {

            if (strpos($k, 'product_id')!==false)
                         $items[substr($k, 10)] = $value;

            elseif (strpos($k, 'quantity')!==false)
                         $items1[substr($k, ] = $value;

   }

   
$n = -1;

foreach($items1 as $k => $v) {
$n = $n + 1;
$product_id = $items[$n];
$quantity = $items1[$n];

$query4 = "INSERT into orders (client_id, product_id, order_number, quantity, date) VALUES ('$client_id', '$product_id', '$order_number', '$quantity', NOW())";
$result4 = @mysql_query ($query4);

if ($result4) {
$query5 = "UPDATE products SET quantity_sold = quantity_sold + '$quantity' WHERE product_id = $product_id";
$result5 = @mysql_query ($query5);
}
}
mysql_close();

Seems to work perfectly, whichever order the $_POST variables arrive in.

All the best,

Rob
Post Reply