Session Problem in Shopping Cart

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nitediver
    New Member
    • May 2010
    • 1

    #1

    Session Problem in Shopping Cart

    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.
    Code:
    switch($action){
    	case "add":
    	$_SESSION['cart'][$prod_id][$size_id]++;
    	break;
    	case "remove":
    	$_SESSION['cart'][$prod_id][$size_id]--;
    	break;
    	case "empty":
    	session_destroy();
    	break;
    }
    header("location:3.php");
    ?>
    3.php, This page for displaying result.
    Code:
    <?
    	foreach($_SESSION['cart'] as $item=>$sizes){
    		foreach($sizes as $size){
    		$prod = mysql_query("SELECT prod_name,size_id,size_number FROM a_prod,a_size WHERE prod_id='$item' AND size_id='$size'");
    			if(mysql_num_rows($prod) > 0){
    			list($prod_name, $size_id, $size_number) = mysql_fetch_row($prod);
    				echo "Name : ".$prod_name." - "."Size : ".$size_number." - "."Qty : ".$size.
    				"<a href=\"2.php?prod_id=$prod_id&size_id=$size_id&action=remove\"> - </a><br>";
    			}
    		}
    	}
    ?>
    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($_SESS ION['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.
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

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

    Comment

    Working...