Hello,
I am relatively new to PHP but I am learning fast. I still have a long way to go. And, I apologize for the length of this thread.
I took on the task of building a shopping cart partially because I needed one for a demo and I thought it would be a great way to learn. With some starts and stops, I have been managing to get thru a lot of it. However, I am at the actual cart portion of this application and I am finding myself stumped or at least stalled. Perhaps a fresh set of eyes on my problem can help me out. Essentially, I have this piece of code that is supposed to take input from an 'Add To Cart' button on a catalog page and if a cart is empty or does not exist, the globals $cart, $item, $item_price are registered with the session. The product sku number is sent to this script like so when the 'Add To Cart' is clicked: cart.php?new=20030001. So my code is as follows: [register_globals = off]
<?php
session_start();
include('assets/includes/pc01_store_fns.php');
//do_html_header('Your Shopping Cart');
if($GET['new'])
{
$new=$GET['new'];
//new item selected
if(!session_is_registered("cart"))
{
session_register("cart");
$cart = array();
$items = 0;
session_register("items");
$total_price = "0.00";
session_register("total_price");
$sessionId = session_id();
}
$cart=$HTTP_SESSION_VARS['cart'];
if($cart[$new])
$cart[$new]++;
else
$cart[$new] = 1;
$total_price = calculate_price($cart);
$items = calculate_items($cart);
}
if($save)
{
foreach ($cart as $itemSku => $qty)
{
if($$itemSku=="0")
unset($cart[$itemSku]);
else
$cart[$itemSku] = $$itemSku;
}
$total_price = calculate_price($cart);
$items = calculate_items($cart);
}
?>
The theory here is that if the item is already in the cart ;
if($cart[$new])
$cart[$new]++;
then the quantity is adjusted up by a unit of 1. If it is not in the cart;
else
$cart[$new] = 1;
then it is added with a quatity of 1. What is actually happening is:
if I add say the first item in a category, it adds to the cart and displays fine with the proper quantity. If I 'Continue Shopping' and add the same item again, it updates the cart and now shows a quantity of 2. This is correct. If, however, I do it again .. the quantity remains unchanged. It is not a display code error as I check the ontents of the $cart array and it has not been updated. I do not get this.
Then if I add say the second item on the category page at this point, it adds a second item to the cart and displays it fine with a quantity of 1 but it changes the quantity of the first item added to the cart back to 1.
Here is my calculate_items code which is in an include file:
<?php
session_start();
function calculate_items($cart)
{
// sum total items in shopping cart
$items = 0;
if(is_array($cart))
{
foreach($cart as $items => $qty)
{
$items += $qty;
}
}
return $items;
}
?>
I know this is going to be a stupid rookie coding mistake but I have been too close to this for too long and I am not seeing it so if someone out there can take the time to help an amateur out here ... I would sincerely appreciate it.
Cheers
Dave