hmmm... If I was you.. I would make your cart an object.. build a simple
class like:
class Cart
{
var $items;
function Cart()
{
$this->items=Array();
}
function Add_To_Cart($item)
{
if(!in_array($item,$this->items)
{
Array_Push($this->items,$items;
}
}
function Remove_From_Cart($item)
{
if(in_array($item,$this->items)
{
unset($this->item[$items);
}
}
}
So in your script:
session_start();
if (!session_is_registered("cart"))
{
$cart = new Cart;
session_register("cart");
}
/* now you should be able to get everything like this */
$cart->Add_To_Cart($whatever);
$cart->Remove_From_Cart($whatever);
/* where $whatever is the array of item data... first of which would be the
id */
then you can manipulate it how ever you like.. just make the function inside
the cart clas and call it by $cart->function_name();
This way the only thing stored in the session is the object.. which contains
it all in place you can handle it. This is the only way I got reliable use
out sessions :p
I just whipped this off the top of my head.. so may not work :)
JD
-Original Message-
From: Robert Weeks [mailto:[EMAIL PROTECTED]]
Sent: Thursday, February 07, 2002 10:41 AM
To: php
Subject: Re: [PHP-DB] Viewing Session Varibles
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:
++
$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 "Title: $title";
echo "Description: $p_desc";
echo "Price: $price";
echo "Quantity: $qty";
echo "";
}
}
#print varibles to screen for debugging
echo"";
print_r($HTTP_SESSION_VARS);
echo"";
?>
+
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
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php