-1

I am working on ecommerce cart page. I already done the cart but I need the grand total. Please help

  <?php
    $query = mysqli_query($conn,"select * from cart where usr_id = '".$_SESSION['id']."'");
    while($row=mysqli_fetch_array($query))
    {
        $per_price = $row['price'];
        $per_quantity = $row['quantity'];    
        $total_amount = $per_price * $per_quantity;    
  ?>


  <tbody>
    <tr>
      <td><?php echo $row['name']?></td>
      <td>RM <?php echo $row['price']?></td>
      <form class="no-margin" method="post"> 
      <td><input class="form-1" type="number" name="quantity" value="<?php echo $per_quantity?>"></td>
      <td>RM <?php echo $total_amount?></td>
    </tr>
  </tbody>
<?php
}
?>
Machavity
  • 30,841
  • 27
  • 92
  • 100
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Jul 13 '22 at 19:01

1 Answers1

0

Put a new variable to store all prices :

<?php
    $total_amount_all = 0;
    $query = mysqli_query($conn,"select * from cart where usr_id = '".$_SESSION['id']."'");
    while($row=mysqli_fetch_array($query))
    {
        $per_price = $row['price'];
        $per_quantity = $row['quantity'];
        $total_amount = $per_price * $per_quantity;
        $total_amount_all += $total_amount;
?>

<tbody>
    <tr>
        <td><?php echo $row['name']?></td>
        <td>RM <?php echo $row['price']?></td>
        <form class="no-margin" method="post"> 
        <td><input class="form-1" type="number" name="quantity" value="<?php echo $per_quantity?>"></td>
        <td>RM <?php echo $total_amount?></td>
    </tr>
</tbody>

<?php
    }
?>

<p><?=$total_amount_all?></p>
Liam-Nothing
  • 167
  • 8