Ok,

This is driving me nuts. I'm sure its something simple but I can't seem to
find the glitch.

I'm trying to make a simple shopping cart using Session varibles to store
the item => quantity pairs, then I loop thru the cart and query the db to
get the item details, etc.. Most of it works fine but whenever I add a new
item to the cart I lose the $qty value for the other items.

Here is the code in question:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

<?

session_start();

include 'inc/common.inc.php';

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

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

foreach ($cart as $id => $qty)
{
if ($$id == "0")
{
 unset ($cart[$id]);
}else{
 $cart[$id] = $$id;
}

$items = calculate_items($cart);

foreach ($cart as $id => $qty){

$db = mysql_connect("localhost", "$databaseuser", "$databasepasswd") or die
("Unable to connect to database!");

mysql_select_db ("$databasename",$db) or die ("Unable to connect to database
$db!");

$result = mysql_query ("SELECT * from products WHERE id = '$id'") or die
("Error in query. " . mysql_error());

if ($result){

extract(mysql_fetch_array($result));

echo "<b>Title:</b> $title<br />";
echo "<b>Description:</b> $p_desc<br />";
echo "<b>Price:</b> $price<br />";
echo "<b>Quantity:</b> $qty<br />";
echo "<hr />";
 }
}

#print varibles to screen for debugging

echo"<pre>";
print_r($HTTP_SESSION_VARS);
echo"</pre>";

?>

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

If one item is in the cart I get this from the $HTTP_SESSION_VARS:

Array
(
    [cart] => Array
        (
            [10] => 12
        )

    [items] => 12
    [total_price] => 0.00
)

That would indicate to me that the cart pair has been saved.
But as I add items I lose the earlier quantities:

Array
(
    [cart] => Array
        (
            [10] =>
            [16] =>
            [13] => 6
        )

    [items] => 6
    [total_price] => 0.00
)

The $id is still there and I can query the db just fine, but no quantity.

I don't know why this is eluding me but any help would be greatly
appreciated.

Thanks,

Robert


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to