Form -> Shopping Cart updating.

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
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Form -> Shopping Cart updating.

Post by tecktalkcm0391 »

I have a form that has values for each item like this:

Code: Select all

<input type="text" name="qty1" value="2" size="4" maxlength="4" title="Order Quantity" />
<input type="checkbox" name="remove1" value="true">
For the quantity the name is qty(item number) and value equals the amount.
For the remove the name is remove(item number).

The cart is in a $_SESSION like this: 1,1 for the above HTML
a fulled cart would be 1,1,2,2,5,7,7

Now my question is how can I make it so it updates the quantity and removes all of the items from the cart.
I already have this so far, plus more that doesn't work and is badly designed.

Code: Select all

$qty = array();
$remove = array();
foreach ( $_POST as $key => $value ) {
	if(substr($key, 0, 6)=='remove'){
		if($value=='true'){
			$remove[] = substr($key, 6);
		}
	} elseif(substr($key, 0, 3)=='qty'){
		$qty[] = array(substr($key, 3),$value);
	} 
}
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

You may want to arrange your inputs as arrays.

Code: Select all

<input type="text" name="qty[]" />
<input type="text" name="remove[]" />
<input type="text" name="qty[]" />
<input type="text" name="remove[]" />
print_r($_POST) too see how to use it.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

For shopping cart updates (by which I mean changes to the cart from the cart where there can be multiple operations at once) I usually implement an instruction pipeline where I determine all the operations and then process the deletes, inserts, and quantity/quality changes in separate phases.

You can use arrays as suggested or create names with information in them and parse the names.
(#10850)
Post Reply