![]() |
Looping Through Variable within $_SESSION Variable
I am attempting to create an ecommerce site from scratch so that I can include it in my portfolio and eventually apply for an entry-level web development job and I am running into an issue regarding the $_SESSION.
When a user adds an item to their cart I am having the script loop through the $_SESSION elements that look like $_SESSION['item_x'] where 'x' is a number. Once it finds an unused number then I want that variable to be set equal to the product_id. For example if there were nothing already in the cart then, assuming 'x' starts at 0, $_SESSION['item_0'] would be set equal to the product_id that is being added to the cart. Currently my code looks like this: PHP Code:
Any help would be appreciated. If I am making this much more difficult than is necessary then please let me know. |
Both will work just fine. But the delimiter option involves less code for the same amount of logic when calculating the products in the cart and total cost at the end. e.g $_SESSION['items'][] = $item_product_no.",".$item_quantity.
Also, instead of using something like - $_SESSION['item_' . $n] --> $_SESSION['item_1'], instead add another dimension to make retrieval even easier at the end. For example: $_SESSION['items'][$i] --> $_SESSION['items'][1] A simple foreach will make it easy to retrieve it. PHP Code:
|
| All times are GMT +1. The time now is 02:11 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.