|
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>";
?>
|