session variable help?

    Date: 10/04/05 (PHP Community)    Keywords: php, mysql, sql, web, shopping

    Context: I'm working on a bare-bones shopping cart system and am using a session variable $cart to store cart items. $cart is an associative array holding item_id => quantity. To add an item to the cart, I'm calling the page with a url variable ?new=xx where xx is the item id. If the item id exists in the array, am incrementing it. If it doesn't exist, I create it and set the quantity to 1. In theory, anyway.

    Problem: Adding a new item to the cart produces the expected results, but if the page is called immediately after without the url variable, or with a url variable pointing to a different id, it increments the quantity of the item just added by one. It is also the case that when I call a separate checkout page with the display_cart($cart) function in it, the item quantity shows up incremented by one.

    (the following code will look very familiar if you've ever used PHP and MySQL Web Development)


    On the shopping cart page:

    if($new)
    {
        if(!session_is_registered("cart"))
        {
            $cart = array();
            session_register("cart");
        }

        if($cart[$new])
             $cart[$new]++;
        else
            $cart[$new] = 1;
    }

    if($save) //have tried commenting out this part, makes no difference
    {
        foreach ($cart as $item_id => $qty)
        {
            if ($$item_id == "0")
                unset($cart[$item_id]);
            else
                $cart[$item_id] = $$item_id;
        }
    }

    if ($cart&&array_count_values($cart))
        display_cart($cart);
    else
        echo "There are no items in your cart.";


    Display cart function:

    function display_cart($cart)
    {
        echo "

    ";
        echo "";
        echo "";
        echo "";
        echo "";

        foreach($cart as $item_id => $qty)
        {
            $item = get_item_details($item_id);
            echo "";
        }

        echo "";
        echo "
        < td align = center >
        < input type = hidden name = save value = true >
        < input type = image src = \"images/save_changes.jpg\" border = 0 alt = \"Save Changes\">
        
        
        ";
        echo "
    item nameqtyprice
    ";
            echo $item['name'];
            echo "
    ";
            echo "";
            echo "
    ";
            echo $item['price'];
            echo "
    &nbsp;&nbsp;
    ";
        echo "
    ";
    }


    I've done a bit of reading on how sessions work, but am still pretty confused by it. Is this black magic or am I just not going about this right? Any suggestions, including debugging techniques, would be appreciated. In the meantime, I'm just going to take a whole different approach to this cart, but I'd like to solve the mystery eventually.

    Thanks in advance.

    Source: http://www.livejournal.com/community/php/350281.html

« poor performance... || PHP and Forms »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home