Page 1 of 1
Session Problem in Shopping Cart
Posted: Sat May 29, 2010 2:56 am
by nitediver
This is simple shopping cart, it's using session to carry data.
It using three different page:
2.php : Use for passing data to 3.php
3.php : Display the data, iterate value using foreach().
I try to put if around the case "add" on 2.php, with condition "if the size exist it will only increment the product", but it did not work.
Re: Session Problem in Shopping Cart
Posted: Sat May 29, 2010 4:21 am
by internet-solution
session_start() should be the first line after <? tag.
Re: Session Problem in Shopping Cart
Posted: Sat May 29, 2010 4:57 am
by Brenden
Perhaps your main point of concern is this
Code: Select all
$_SESSION["cart"][$prod_id]++;
$_SESSION["cart"][$size_id]++;
prod_id and size_id are taken from two different tables and represent two different things, yet you're storing them both in exactly the same way, as $_SESSION["cart"][11] or $_SESSION["cart"][2] - which is the product and which is the size? What if they both have the same ID? Perhaps you could store them like this instead
Code: Select all
$_SESSION["cart"][$prod_id][$size_id]++;
This will give you a unique array location for each distinct item/size combination. Then you can loop through them like this
Code: Select all
foreach ($_SESSION["cart"] AS $item->$sizes) {
foreach ($sizes AS $size) {
// $item is product id
// $size is size id
}
}
I'm getting really annoyed at this forum sanitising my characters in php code, so you'll have to trust me that 40 is open round bracket and 41 is close round bracket
Re: Session Problem in Shopping Cart
Posted: Sat May 29, 2010 9:01 am
by nitediver
I thought It would not be able if I do this, I ever ask someone about this:
Code: Select all
$_SESSION["cart"][$prod_id][$size_id]++;
Thanks I will try this:
Code: Select all
foreach ($_SESSION["cart"] AS $item->$sizes) {
foreach ($sizes AS $size) {
// $item is product id
// $size is size id
}
}
Re: Session Problem in Shopping Cart
Posted: Sun May 30, 2010 8:35 am
by nitediver
It work, but something weird happen, for the first submission it appear to be normal. On the second submission and so on(with same product & size), the size_id is increment [1]40 > [2]41 > [3]42:
1.First submission : Fade 40 >> Name : Fade - Size : 40 - Qty : 1 -
2.Second submission: Fade 40 >> Name : Fade - Size : 41 - Qty : 2 -
Since I only have 3 size in my table, when the product submit four times it display nothing.
It also happen for same product but different size, keep getting the first size(40) in table rather chosen one.
It display these:
Name : Sonar - Size : 40 - Qty : 1 -
Name : Sonar - Size : 40 - Qty : 1 -
Rather than this:
Name : Sonar - Size : 40 - Qty : 1 -
Name : Sonar - Size : 42 - Qty : 1 -
I try to put if around the case "add" on 2.php, with condition "if the size exist it will only increment the product", but it did not work.
Re: Session Problem in Shopping Cart
Posted: Tue Jun 01, 2010 7:57 am
by nitediver
Anyone any idea, please.