开发者

help with multi dimentional arrays

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.


$_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.



$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.


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜