views:

38

answers:

3

hi, i am building a shopping cart and cant figure out how to store something like this into a session.

[product_id1] = quantity;
[product_id1] = size
[product_id1] = color;

[product_id2] = quantity;
[product_id2] = size;
[product_id2] = color;
...
etc

so when a user select the quantity of a product then selects its color then selects to add to a cart i want the items selected to be added into a session and each item added to the cart , its attributes selected to be added into a session. how would i do this?

many many thanks.

A: 
$_SESSION['productid1']['quantity'] = 15;
$_SESSION['productid1']['size'] = 30;
$_SESSION['productid1']['color'] = 'red';

$_SESSION['productid2']['quantity'] = 35;
$_SESSION['productid2']['size'] = 2;
$_SESSION['productid2']['color'] = 'blue';

Don't forget to put session_start() at the beginning of every page to carry the sessions through the pages.

Duniyadnd
What happens when you want a productid1 in red and another productid1 in green?
Syntax Error
wont be possible productid1 is actually an id of a product. so all attribs selected for product1 are selected through html fields. but then updated when a user selects otherwise.
sarmenhb11
Yes, that was my point. If the customer wants to buy two widgets with the same product_id (one in red, and another in green) then it wont work. The color will be overwritten as the second choice, and the customer won't get what they want.
Syntax Error
oh, your right. thanks for pointing that out. i am having a big problem with understanding multi arrays even though i have studied and worked with it multiple times. can we talk on an instant messaging program by any chance? i use aim my screen name is sarmenhb
sarmenhb11
actually i thought about it anad it would only replace the added value if i checked to see if that value existed and updated it with thet new one. but if i just appended another product id that existed into the array then i would be fine.
sarmenhb11
I was under the impression that productid1/productid2 were just instances of various products, whether it was the same or different. Same way, the user can add a $_SESSION['productid1']['productID'] value of what the actual product is.
Duniyadnd
A: 

You should create an array in session array for your products:

$_SESSION['products'] = Array();

then you can put products there like this:

$product = Array();
$product['quantity'] = 6;
$product['size'] = 'XXL';
$product['color'] = 'blue';

$_SESSION['products'][] = $product;

$product = Array();
$product['quantity'] = 2;
$product['size'] = 'XL';
$product['color'] = 'blue';

$_SESSION['products'][] = $product;

this will give you numbered array, if you want an associative array, you will just put identifier into []:

$_SESSION['products']['productID'] = $product;
praksant
A: 

$item[$catalog_number]['quantity'] = 1;
$item[$catalog_number]['size'] = 'XL';
$item[$catalog_number]['color'] = 'yellow';
$_SESSION['cart'][] = $item;
unset($item);

Repeat for each item you are adding. Alternatively you could do:


$item['catalog_number'] = 'ABC-123';
$item['quantity'] = 1;
$item['size'] = 'XL';
$item['color'] = 'yellow';
$_SESSION['cart'][] = $item;
unset($item);

Both will work, just make sure you are consistent. Use only one or the other.

Syntax Error
thank you! how would i add this to a session?
sarmenhb11
As @Duniyadnd said, put session_start() at the beginning of every page. Assigning to the $_SESSION variable then puts it in the session.
Syntax Error