Hi,
I am stuggling with the last part of a shopping cart and specifically, the script which updates the cart when the user changes the quantity. For some reason (probably obvious ... but not to me at this point), when I click on save changes, the array that is stored as a session var called 'cart' is getting its key values set to 0. I am hopping some of you could have a quick glance thru my scripts and see if you can spot where I am screwing up.
Cart Script: (sorry for not using the format function here but it does not seem to be working)
<?php
session_start();
include('assets/includes/pc01_store_fns.php');
//echo "<font color=\"#FFFFFF\">The itemSku is " . $GET['itemSku'] . "</font><br>";
if($GET['itemSku'])
{
$itemSku = $GET['itemSku'];
//new item selected
if(!isset($SESSION['cart']))
{
//echo "<br><font color=\"#FFFFFF\">A session has not yet been registered. We will do that now!</font>";
$SESSION['cart'] = array();
$SESSION['items'] = 0;
$SESSION['total_price'] = "0.00";
}
//echo "<br><font color=\"#FFFFFF\">Session is registered.</font><br>";
if($SESSION['cart'][$itemSku]) {
//echo "<font color=\"#FFFFFF\">Item chosen is already in the cart.</font><br>";
$SESSION['cart'][$itemSku]++;
//print_r($SESSION['cart']);
//echo "<br>";
} else {
$SESSION['cart'][$itemSku] = 1;
//print_r($cart);
}
$total_price = calculate_price($SESSION['cart']);
$items = calculate_items($_SESSION['cart']);
}
if($POST['save']) //(IT IS THIS SCRIPT HERE THAT IS GIVING ME TROUBLE. PART OF IT'S FUNCTIONALITY COMES FROM THE INCLUDE FILE TO FOLLOW)
{
echo "<font color = \"#FFFFFF\">the cart has been changed and saved.</font><br>";
var_dump($SESSION['cart']);
foreach ($_SESSION['cart'] as $itemSku => $qty)
{
echo "<font color = \"#FFFFFF\" size = 1>The quantity of $itemSku is $qty</font><br>";
if($$itemSku=="0")
unset($_SESSION['cart'][$itemSku]);
else
$_SESSION['cart'][$itemSku] = $$itemSku;
}
$total_price = calculate_price($_SESSION['cart']);
$items = calculate_items($_SESSION['cart']);
}
?>
The following is the function to display the cart - found on the include file:
function display_cart($cart, $change = true, $images = 0)
{
// display items in shopping cart
// optionally allow changes (true or false)
// optionally include images (1 - yes, 0 - no)
//global $items;
//global $total_price;
echo "<table border = 0 width = 100% cellspacing = 0>
<form action = cart.php method = post>
<tr class=\"tbleHdr\"><th align=left colspan = ". (1+$images) .">Item</th>
<th>Price</th><th>Quantity</th>
<th>Total</th></tr>";
//display each item as a table row
foreach ($SESSION['cart'] as $itemSku => $qty)
{
$items = get_item_details($itemSku);
echo "<tr>";
if($images ==true)
{
echo "<td align = left>";
if (file_exists("images/$itemSku.jpg"))
{
$size = GetImageSize("images/".$itemSku.".jpg");
if($size[0]>0 && $size[1]>0)
{
echo "<img src=\"images/".$itemSku.".jpg\" border=0 ";
echo "width = ". $size[0]/3 ." height = " .$size[1]/3 . ">";
}
}
else
echo " ";
echo "</td>";
}
echo "<td class=\"record\" align = left>";
echo "<a href = \"order_a.php?itemSku=".$itemSku."\">".$items["itemName"];
echo "</td><td class=\"record\" align = center>$".number_format($items["itemPrice"], 2);
echo "</td><td class=\"record\" align = center>";
// if we allow changes, quantities are in text boxes
if ($change == true)
echo "<input type = text class=\"record\" name = \"$itemSku\" value = $qty size = 3>";
else
echo $qty;
echo "</td><td class=\"record\" align = center>$".number_format($items["itemPrice"]*$qty,2)."</td></tr>\n";
}
// display total row
echo "<tr class=\"tbleHdr\">
<th class=\"tbleHdr\" colspan = ". (2+$images) ."> </th>
<th class=\"tbleHdr\" align = center>" . $SESSION['items'] . "
</th>
<th class=\"tbleHdr\" align = center>
\$".number_format($SESSION['total_price'], 2).
"</th>
</tr>";
// display save change button
if($change == true)
{
$target = "index.php";
echo "<tr>
<td align = center>" ?> <?php display_button("NULL", $target, "continue-shopping", "Continue Shopping"); ?> <?php echo "</td>
<td align = center>" ?> <?php $path = $SERVER['PHP_SELF'];
//echo "$path";
$path = str_replace("cart.php", "", $path);
display_button("NULL", "https://".$_SERVER['SERVER_NAME'].$path."checkout.php", "go-to-checkout", "Go To Checkout"); ?> <?php echo "
<td align = center>
<input type = hidden name = save value = true>
<input type = image src = \"assets/images/save-changes.gif\"
border = 0 alt = \"Save Changes\">
</td>
<td> </td>
</tr>";
}
echo "</form></table>";
}
So the cart displays and functions as expected until we update the cart. The 'Save Changes' script is basically supposed to take the value from the quantity field and if the value is 0, unset the entry from $_SESSION['cart'], otherwise, update the $qty value to the new value. I think I have been staring at this too long and can't see it. I would REALLY appreciate some help here.
Thanks much
Dave