Hi, I'm facing a problem which is can't total up all the subtotal amount. Everytime I add a new product to cart, the total only show the last product that I added into cart. Not only that, when I update the quantity of other product, the total still remain the last product price.
The picture above you can see the total only show the amount 21. Any solution?
<?php
include('./include/config.php');
include('./include/loginverify.php');
?>
<!DOCTYPE html>
<!-- Website template by freewebsitetemplates.com -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Artist Website Template</title>
<link rel="stylesheet" href="images/css/style.css" type="text/css" charset="utf-8" />
</head>
<body>
<span id="background"></span>
<div id="page">
<div id="sidebar">
<div id="logo">
<a href="index.html">Welcome <p><em><?php echo $_SESSION['username']?></em></p></a>
</div> <!-- /#logo -->
<ul id="navigation">
<li class="selected"><a href="home.php">Product List</a></li>
<li><a href="member_list.php">Member List</a></li>
<li><a href="top10.php">Top 10</a></li>
<li><a href="Report">Report</a></li>
<li><a href="logout.php">log out</a></li>
</ul> <!-- /#navigation -->
<ul id="connect">
<li><a href="http://facebook.com/" target="_blank" class="facebook"></a></li>
<li><a href="http://twitter.com/" target="_blank" class="twitter"></a></li>
<li><a href="" class="link-us"></a></li>
</ul> <!-- /#connect -->
<div class="footer">
© Copyright © 2011.<br/>
<a href="index.html">D&W CD Rental Store</a> all rights reserved.
</div> <!-- /.footer -->
</div> <!-- /#sidebar -->
<div id="contents">
<!-- HTML Codes by Quackit.com -->
<p style="text-align:center;"><span style="font-family:Arial;font-size:19px;font-style:normal;font-weight:normal;text-decoration:none;text-transform:uppercase;color:000000;background-color:ffffff;">Product List</span>
<table class="hovertable">
<tr>
<th>ID</th>
<th>Name</th>
<th>Quantity</th>
<th>Subtotal</th>
</tr>
<tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
<?php
//determind add action
if(isset($_GET['action']) && ($_GET['action'] == 'add')){
//determind ID from url
if(isset($_GET['id'])){
//determind the value is positive or not
if($_GET['id']> 0){
//declare ID as $_GET['ID'] from url
$id =(int) $_GET['id'] ;
$query = "SELECT * FROM product WHERE ID = $id";
$result = mysql_query($query,$con);
if(isset($_SESSION['cart'][$id])){
$_SESSION['cart'][$id]++;
echo'Another copy has been added to cart';
}
else{
$_SESSION['cart'][$id] = 1;
echo'This item has been added to your cart';
}
}
}
}
//if the action is update in url
elseif(isset($_POST['action']) && ($_POST['action'] =='update')){
//
foreach ($_POST['qty'] as $id => $v){
$qty = (int)$v;
if ($qty > 0){
$_SESSION['cart'][$id] =$qty;
}
if($qty == 0){
unset($_SESSION['cart'][$id]);
}
}
echo'Your cart has been updated';
}
?>
<?php
if($_SESSION['cart']) { //if the cart isn't empty
//show the cart
//iterate through the cart, the $product_id is the key and $quantity is the value
foreach($_SESSION['cart'] as $id => $v) {
//get the name, description and price from the database - this will depend on your database implementation.
//use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
$sql = sprintf("SELECT * FROM product WHERE id = %d;",
$id);
$result = mysql_query($sql);
//Only display the row if there is a product (though there should always be as we have already checked)
if(mysql_num_rows($result) > 0) {
//list($Name, $Rent_price, $Category) = mysql_fetch_row($result);
while($row = mysql_fetch_array($result)){
$total = 0;
$line_cost = $_SESSION['cart'][$row['ID']] * $row['Rent_price']; //work out the line cost
$total =$line_cost;
//add to the total cost
echo'<form action="cart.php" method="post">
<input type="hidden" name="action" value="update" />';
echo '<td>' . $row['ID'] . '</td>';
//show this information in table cells
echo '<td>' . $row['Name']. '</td>';
//along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current product
echo "<td><input type = \"text\" size=\"3\" name=\"qty[$id]\" value =\"{$_SESSION['cart'][$id]}\"> </td>";
echo '<td>'.$line_cost.'</td>';
echo "</tr>";
}
}
}
//show the total
echo "<tr>";
echo "<td colspan=\"3\" align=\"right\">Total</td>";
echo "<td align=\"left\">$total</td>";
echo "</tr>";
}else{
//otherwise tell the user they have no items in their cart
echo "You have no items in your shopping cart.";
}
function productExists($id) {
//use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
$sql = sprintf("SELECT * FROM product WHERE id = %d;", $id);
return mysql_num_rows(mysql_query($sql)) > 0;
}
echo "<td colspan=\"3\" align=\"right\"><input type=\"submit\" value=\"update\"></td>";
?>
</form>
</table>
</div> <!-- /#contents -->
</div> <!-- /#page -->
</body>
</html>
</body>
</html>