Session Problem in Shopping Cart

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Session Problem in Shopping Cart

Post 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.
Last edited by nitediver on Wed Jun 09, 2010 7:27 am, edited 5 times in total.
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

Re: Session Problem in Shopping Cart

Post by internet-solution »

session_start() should be the first line after <? tag.
Brenden
Forum Newbie
Posts: 12
Joined: Fri May 28, 2010 2:12 am

Re: Session Problem in Shopping Cart

Post 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
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: Session Problem in Shopping Cart

Post 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
}
}
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: Session Problem in Shopping Cart

Post 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.
Last edited by nitediver on Wed Jun 09, 2010 7:29 am, edited 2 times in total.
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: Session Problem in Shopping Cart

Post by nitediver »

Anyone any idea, please.
Post Reply