Page 1 of 1

Multidimensional arrays shopping cart question

Posted: Mon Jul 07, 2003 5:05 pm
by m@ndio
Hi there,

How can I automatically add another array to an existing one hence making it multidimensional like an add to cart script..

In my script each array has a; category, description and price like so:

Code: Select all

//create array
$arr_prod = array(Category=>$f_cat,
Description=>$f_desc,
Price=>$f_price);
as you can see the first one goes obviously goes in there fine but when the form in my script is re-submitted it overwrites the existing stuff.. how can I end up with the following:

Code: Select all

$arr_prod = array(
                           array(Category=>"shoes",
                           Description=>"these are shoes",
                           Price=>49.99),
                           array(Category=>"trainers",
                           Description=>"these are trainers",
                           Price=>39.99),
                           array(Category=>"t-shirts",
                           Description=>"these are t-shirts",
                           Price=>19.99),

);
Do I really have to use sessions?? :?

Posted: Mon Jul 07, 2003 5:56 pm
by bionicdonkey
i think you want to look at array_push()

Description
int array_push ( array array, mixed var [, mixed ...])


array_push() treats array as a stack, and pushes the passed variables onto the end of array. The length of array increases by the number of variables pushed. Has the same effect as: $array[] = $var;

repeated for each var.

Returns the new number of elements in the array.

Example 1. array_push() example

$stack = array ("orange", "banana");
array_push ($stack, "apple", "raspberry");


This example would result in $stack having the following elements: Array
(
[0] => orange
[1] => banana
[2] => apple
[3] => raspberry
)

Posted: Mon Jul 07, 2003 5:59 pm
by McGruff
$array_prod starts off as a single product array, then becomes a multi-dimensional array storing several products. Although you can store different types of data in the same var name as you run through a script, the script might be clearer with a separate, $order array to store the selected products (a multi-dimensional array).

Each time the customer clicks an "add to cart" button:

$i = count($order);
$order[$i] = $array_prod;

Array keys will start at zero (first time they click "add to cart" count($order) == 0).

You'll need to initialise $order = array(); before all that to avoid undefined variable errors.

To remove an item from the cart, you could pass the appropriate $key number in a hidden form field then delete that key from $order. Next, you'd need to do something to the array to restore consecutive 0, 1, 2, 3 etc keys however (or $order[$i] = $array_prod; will overwrite a value). Actually, I think you can do all that in one go with array_splice() but can't remember exactly.

Dump sessions? An alternative would be to track user with a cookie and store/retrieve $order from db each time they adjust their shopping cart. That creates more db load however.