By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,567 Members | 1,522 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,567 IT Pros & Developers. It's quick & easy.

Session Problem in Shopping Cart

P: 1
I have problem with this simple shopping cart, I am going to pushing two different value from different table into arrays.
The value is product id & size id, its work to carry the value but the size appear not to be fix inside the cart.

2.php, This page for pushing value inside arrays.
Expand|Select|Wrap|Line Numbers
  1. switch($action){
  2.     case "add":
  3.     $_SESSION['cart'][$prod_id][$size_id]++;
  4.     break;
  5.     case "remove":
  6.     $_SESSION['cart'][$prod_id][$size_id]--;
  7.     break;
  8.     case "empty":
  9.     session_destroy();
  10.     break;
  11. }
  12. header("location:3.php");
  13. ?>
  14.  
3.php, This page for displaying result.
Expand|Select|Wrap|Line Numbers
  1. <?
  2.     foreach($_SESSION['cart'] as $item=>$sizes){
  3.         foreach($sizes as $size){
  4.         $prod = mysql_query("SELECT prod_name,size_id,size_number FROM a_prod,a_size WHERE prod_id='$item' AND size_id='$size'");
  5.             if(mysql_num_rows($prod) > 0){
  6.             list($prod_name, $size_id, $size_number) = mysql_fetch_row($prod);
  7.                 echo "Name : ".$prod_name." - "."Size : ".$size_number." - "."Qty : ".$size.
  8.                 "<a href=\"2.php?prod_id=$prod_id&size_id=$size_id&action=remove\"> - </a><br>";
  9.             }
  10.         }
  11.     }
  12. ?>
  13.  
Question:
1)On the first submission, if I choose size 42, the result display 40 then increment on the next submission and so on.
Since I only have 3 size in my table, when the product submit four times it display nothing.

2)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 -

Someone told me to use var_dump to see the array, but I don't know how to use it to fix my problem.
on : var_dump($_SESSION['cart']);
I got this:
array(1) { [11]=> array(1) { [1]=> int(1) } }
array(1) { [11]=> array(1) { [1]=> int(2) } }

I hope anyone in this forum will help me, because I really need.
Thanks.
May 30 '10 #1
Share this Question
Share on Google+
1 Reply


dlite922
Expert 100+
P: 1,584
I don't see your questions, so I don't know what the problem with your code is.

var_dump is used to debug your code, to see what a variable contains so you can see if it's wrong, you'll know where it went wrong instead of waiting till end of the program.


Dan
Jun 4 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.