Hi,
I am using session variables for storing items for my shopping cart. How should I use session vars to store the Item ID and its Quantity.
Thanks
Shopping Cart
Moderator: General Moderators
I am looking for a way to store in user session what ever user buy. I need to keep Item ID and QTY in user session so when the user check out I can bill the user properly and can send th eappropretaite qunatity to the user.Stryks wrote:What have you got set up so far?
Show a little code to give us something to work with.
Cheers
Thanks
Well .... I'd probably just create an array of the data to be carried and assign it to the session.
You can build the $item array from a database call when a user clicks the item's 'buy now' button and add it to the session as above.
If you know how arrays work, then you'll know that you now have an item saved to $_SESSION['CART'][0]. Adding another item in the same way will give you [1], then [2], etc etc. allowing you to have multiples of multiple items.
Then you could display cart contents with
With access to the slot_id you can do things like increase a slots quantity if the user clicks the appropriate button.
To be really neat about it though, and to save your fingers from typing $_SESSION all over the place, you could build all of this into a class. If nothing else you'll prevent yourself getting freaked out thinking about how much you're tying yourself up with values in your sessions.
Hope this helps.
Code: Select all
$_SESSION['CART'] = array();
$item = array('P_ID'=>120, 'title'=>'Blue Widget', 'qty'=1, 'unit_cost'=>55);
$_SESSION['CART'][] = $item;If you know how arrays work, then you'll know that you now have an item saved to $_SESSION['CART'][0]. Adding another item in the same way will give you [1], then [2], etc etc. allowing you to have multiples of multiple items.
Then you could display cart contents with
Code: Select all
foreach($_SESSION['CART'] as $cart_id=>$cart_item) {
echo "Item $cart_id - (item {$cart_item['P_ID']}){$cart_item['qty']} x {$cart_item['title']} @ ${$cart_item['unit_cost']} each <br />";
}Code: Select all
$_SESSION['CART'][0]['qty'] += 1;Hope this helps.
Either way, the principal is pretty much the same.
Instead of accessing the session for your cart data, you'd access the database instead.
The suggested class implementation would be a champ for this too. You would just have it load the cart data when the class is instantiated, and update on change.
Have a tinker anyhow, and post back if you need some help with code.
Instead of accessing the session for your cart data, you'd access the database instead.
The suggested class implementation would be a champ for this too. You would just have it load the cart data when the class is instantiated, and update on change.
Have a tinker anyhow, and post back if you need some help with code.
Sorted . Thanks so muchStryks wrote:Well .... I'd probably just create an array of the data to be carried and assign it to the session.
You can build the $item array from a database call when a user clicks the item's 'buy now' button and add it to the session as above.Code: Select all
$_SESSION['CART'] = array(); $item = array('P_ID'=>120, 'title'=>'Blue Widget', 'qty'=1, 'unit_cost'=>55); $_SESSION['CART'][] = $item;
If you know how arrays work, then you'll know that you now have an item saved to $_SESSION['CART'][0]. Adding another item in the same way will give you [1], then [2], etc etc. allowing you to have multiples of multiple items.
Then you could display cart contents withWith access to the slot_id you can do things like increase a slots quantity if the user clicks the appropriate button.Code: Select all
foreach($_SESSION['CART'] as $cart_id=>$cart_item) { echo "Item $cart_id - (item {$cart_item['P_ID']}){$cart_item['qty']} x {$cart_item['title']} @ ${$cart_item['unit_cost']} each <br />"; }To be really neat about it though, and to save your fingers from typing $_SESSION all over the place, you could build all of this into a class. If nothing else you'll prevent yourself getting freaked out thinking about how much you're tying yourself up with values in your sessions.Code: Select all
$_SESSION['CART'][0]['qty'] += 1;
Hope this helps.