I created a simple cart that uses arrays to store products and prices
$products=array('IBM Laptop', 'Dell Laptop', 'Gateway Desktop', 'Service Contract');
$prices=array(200.00, 300.00, 400.00, 199.99);
I listed the items in a table and then I tried to add a link that deletes that specific tiem in the cart array called
$_SESSION['cart']
the link that deletes the item syntax is
echo "<td><a href=" . $_SERVER['PHP_SELF'] . "?delete=" .$_SESSION['cart'][$n]. ">Delete</a></td>";
and this is the part of the script that is suppose to delete the item
f ($_GET['delete']) {
unset($_SESSION['cart'][$_GET['delete']]);
}
it does no delete the item and the price. somtimes it deletes the item and the prices and sometime it just deletes the item not the price.
I need to understand why this is happening and how can I fix the issue. This is the first part of my final and I have to present it on Friday. I am a newbie and My head is going crazy with this bug . it's been 24hours on four hours sleep trying to solve this issue.
here is a link to the cart.
http://nucitytech.com/store.php
this is the entire code:
<?php
session_start();
if(!isset($_SESSION['cart'])){
$_SESSION['cart']=array();
}
if ($_GET['delete']) {
unset($_SESSION['cart'][$_GET['delete']]);
}
if ($_GET['delete_id']) {
unset($_SESSION['cart'][$_GET['delete_id']]);
}
$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>";
?>:(