Page 1 of 1

Form -> Shopping Cart updating.

Posted: Sat Jun 30, 2007 9:18 pm
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);
	} 
}

Posted: Sat Jun 30, 2007 10:47 pm
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.

Posted: Sat Jun 30, 2007 10:52 pm
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.