Help...
I am using a youtube tutorial to help me create a shopping cart of sorts.
I have a list of products that I want to add to a 'basket' which I have managed to do pretty successfully.
The tutorial uses mysql to gather the information and I am using info from the URL ($_GET) to populate the shopping basket.
Problem comes when i add more than 1 item the price of the previous item changes to the price of the new item.
The code I have used is below
<?php
session_start();
if (isset($_GET['title']))
$title=$_GET['title'];
else
$title=1;
if (isset($_GET['action']))
$action=$_GET['action'];
else
$action="empty";
switch ($action)
{
case "add":
if (isset($_SESSION['cart'][$title]))
$_SESSION['cart'][$title]++;
else
$_SESSION['cart'][$title]=1;
break;
case "remove":
if (isset($_SESSION['cart'][$title]))
{
$_SESSION['cart'][$title]--;
if ($_SESSION['cart'][$title]==0)
unset($_SESSION['cart'][$title]);
}
break;
case "empty":
unset($_SESSION['cart']);
break;
}
/*Display Cart*/
if (isset($_SESSION['cart']))
{
echo "<table width='170' border='0' cellspacing='0' cellpadding='0'>";
foreach($_SESSION['cart'] as $title => $x)
{
$price = $_GET['price'];
$line_cost = $price * $x;
$total = $total+$line_cost;
echo "
<tr>
<td><font color='#ffffff'> $title </font></td>
</tr>
<tr>
<td><div align='right'><font color='#ffffff'> $x <a href='shop.php?title=".$title."&action=remove'>(reduce)</a> x ".$price."</font></div></td>
</tr>
<tr>
<td><div align='right'><font color='#ffffff'>= ".$line_cost."</font></div></td>
</tr>
<tr>
<td> </td>
</tr>";
}
echo "
<tr>
<td><div align='right'><font color='#ffffff'>Total = <b>£ ".$total."<b></font></div></td>
</tr>
</table>";
}
else
echo "No items selected";
?>
The url address that this links to is along the lines of-
http://www.mysite.com/shop.php?action=add&id=1&title=Name of Product&price=39.99
Looking forward to a reply!!
Thanks