I made the following changes to the script: I added
echo "<td><a href=" . <?=$SERVER['PHP_SELF']?> . "?delete=" . <?=$SESSION['cart'][$n] ?> . ">Delete</a></td>";
this send the user back to the same page and add a vaule in the $_GET['delete'] arrary.
Then I added to the top of the script
if ($GET['delete']) { // Test for variable named delete in query string
unset($SESSION['cart'][$_GET['delete']]);
}
This is suppose to delete that specific value from the array thus deleting it from the cart.
It seems to work the first time then it does not work when I try to delete other items.
here is a link to the cart:
http://nucitytech.com/store.php
I am lost I thought that this would delete the item from the cart. Does anyone know what is the problem??
Full Script listed below:
<?php
session_start();
if(!isset($SESSION['cart'])){
$SESSION['cart']=array();
}
if ($GET['delete']) { // Test for variable named delete in query string
unset($SESSION['cart'][$_GET['delete']]);
}
$products=array('IBM Laptop', 'Dell Laptop', 'Gateway Desktop', 'Service Contract');
$prices=array(200.00, 300.00, 400.00, 199.99);
echo "<table cellspacing=20 >";
echo "<tr><th>Item Title</th><TH>Price</th></tr>";
for($n=0;$n<count($SESSION['cart']);$n++){
echo "<tr>";
echo "<td>".$products[$SESSION['cart'][$n]]."</td>";
echo "<td>".number_format($prices[$_SESSION['cart'][$n]],2)."</td>";
echo "<td><a href=" . <?=$SERVER['PHP_SELF']?> . "?delete=" . <?=$SESSION['cart'][$n] ?> . ">Delete</a></td>";
echo "</tr>";
$total = $total + $prices[$_SESSION['cart'][$n]];
}
echo "<td><b>TOTAL</td></b>";
echo "<td>".number_format($total,2)."</td>";
echo "<td><a href=store.php>Continue Shopping</a></td>";
echo "</table>";
?>