0

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 but when I update the last product quantity, the total will shows the last product updated price.

problem6

The picture above you can see the total in the cart only display the amount of the latest added product to the cart but not total up the subtotal. Any solution??

This is the source code:

<?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">
                &copy; Copyright &copy; 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;
            $subtotal = $_SESSION['cart'][$row['ID']] * $row['Rent_price']; //work out the line cost
            $total =$subtotal;
             //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>'.$subtotal.'</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>

Edited by devianleong

3
Contributors
2
Replies
5
Views
5 Years
Discussion Span
Last Post by Szabi Zsoldos
0

On line 122 above, you're overwriting the total with every iteration of your loop.

Create a preset variable $total = 0 on line 103, and then replace line 122 with $total += $subtotal;

Edited by blocblue

0

$total =$subtotal;

U should add the first value from the while()

change 
$total = $subtotal;

to

$total += $subtotal;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.