I create a session variable named $SESSION[cart] to hold the number of items selected in a shopping cart. I then greated a link called <a href=$SERVER[PHP_SELF]?buy=$n>BUY</a> that once selected is suppose to pass the value passed in the buy variable to $SESSION[cart]= $GET[buy]; I thought this would update the number of items in the variable $SESSION[cart] so I created a statement that printed out the results of what is in the cart
$numitems=count($SESSION[cart]);
echo "<td>$numitems Items in Shopping Cart</td> ";
The problem I am having is that every time I selected an Item its not updating the number of items stored in the cart variable.
Can someone tell me what I am doing wrong listed below is the script.
<?php
session_start();
if(!isset($SESSION[cart])){
$SESSION['cart']=array();
}
if(isset($GET[buy])){
$SESSION[cart]= $GET[buy];
header("location: $SERVER[PHP_SELF]");
exit();
}
$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($products);$n++){
echo "<tr>";
echo "<td>$products[$n]</td>";
$price=number_format($prices[$n],2);
echo "<td>$price</td>";
echo "<td><a href=$SERVER[PHP_SELF]?buy=$n>BUY</a></td>";
echo "</tr>";
}
$numitems=count($SESSION[cart]);
echo "<td>$numitems Items in Shopping Cart</td> ";
echo "</table>";
?>