session problems

A

Anonymous

Guest
Im making a shopping cart script, but I cannot figure out why its not working. I have a page, cart.php that uses session variables to keep track of the items a user has in their shopping cart. As soon as the page is reloaded (to be more precise, an action happens, for example an item is added to the cart, it sends it to cart.php), all the items disapear from the cart.
The top of cart.php:
Code:
  require_once("include/cart_inc.php");
  session_start();
  $_SESSION['items'] = 1;
  $_SESSION['category_choice'] = 1;
  $_SESSION['total'] = 1;

then, when an item is added to the cart:
Code:
  $action = $_GET["action"];
  $cat = $_GET["cat"];
  $item = $_GET["item"];
  if(isset($action))
  {
    $items = alter_cart($cat, $items, $item, $action);
  }

alter_cart is the following:
Code:
  function alter_cart($cat, $items, $item, $action)
  {
    global $dbconn;
    $sql = "SELECT * from inventory where id='$item' AND category='$cat'";
    $result = $dbconn->query($sql);
    errortrap($result);
    if($result->numRows() > 0)
    {
      switch($action)
      {
        case("add"):
                    if(!isset($items[$cat][$item]))
                    {
                      $items[$cat][$item] = 0;
                    }
                    $items[$cat][$item]++;
                    break;
        case("remove"):
                    if(isset($items[$cat][$item]))
                    {
                      $items[$cat][$item]--;
                    }
                    if($items[$cat][$item] < 1)
                    {
                      unset($items[$cat][$item]);
                    }
                    break;
        default:
                    break;
      }
    }
    return $items;
  } //end alter_cart

now when I add an item, it displays the shopping cart properly, but as soon as I go back to cart.php, or anywhere else, the items are lost. any ideas?[/code]
 
Back
Top