Odie
Odie

Reputation: 375

How to get total amount of rows on a table?

How to get the total amount of the cart and place it on the total below the table? Should I use JavaScript or just use PHP? Please give me some advice. Thank you.

      <thead>
        <tr>
          <th class="text-center">Product ID</th>
          <th class="text-center">Product Name</th>
          <th class="text-center">Description</th>
          <th class="text-center">Quantity</th>
          <th class="text-center">Price per Unit</th>
          <th class="text-center">Total Amount</th>
        </tr>
      </thead>
      <tbody>


      <?php 

        $selectCart = "SELECT * FROM cart INNER JOIN products ON products.product_id = cart.product_id";
        $execSelectCart = mysqli_query($connection, $selectCart);

        while ($row = mysqli_fetch_array($execSelectCart)) {

          $cartProId = $row['product_id'];
          $cartProName = $row['product_name'];
          $cartProDesc = $row['description'];
          $cartSellPrice = $row['sell_price'];
          $cartQty = $row['quantityCart'];

          $compute = $cartSellPrice * $cartQty;
          $totalAmount = number_format((float)$compute, 2, '.', '');
       ?>

        <tr>
          <td class="text-center"><?php echo $cartProId; ?></td>
          <td class="text-center"><?php echo $cartProName; ?></td>
          <td class="text-center"><?php echo $cartProDesc; ?></td>
          <td class="text-center"><?php echo $cartQty; ?></td>
          <td class="text-center"><?php echo $cartSellPrice; ?></td>
          <td class="text-center"><?php echo $totalAmount ?></td>
          </td>
        </tr>

      <?php } ?>
      </tbody>
    </table>
    <hr>
    <div class="row text-right">
      <div class="col-xs-2 col-xs-offset-8">
        <p>
          <strong>
            Sub Total : <br>
            VAT 12% : <br>
            Total : <br>
          </strong>
        </p>
      </div>
      <div class="col-xs-2">
        <strong>
          $36.00 <br>
          N/A <br>
          <?php echo $totalAmount; ?> <br>
        </strong>
      </div>
    </div>
   </div>

This is my table. You can see that it only get the last row when I echoed $totalamount outside my while loop.

Upvotes: 1

Views: 140

Answers (3)

mior farhan
mior farhan

Reputation: 82

You can set $total_amount = 0 and in the while loop make sure you add increment to the total amount like $total_amount += $compute just make sure you do your number float before you make the increment.just a simple as that

Upvotes: 0

Parth Patel
Parth Patel

Reputation: 3997

This will work in your case.

<?php 

        $selectCart = "SELECT * FROM cart INNER JOIN products ON products.product_id = cart.product_id";
        $execSelectCart = mysqli_query($connection, $selectCart);

        $totalAmount = 0;
        while ($row = mysqli_fetch_array($execSelectCart)) {

          $cartProId = $row['product_id'];
          $cartProName = $row['product_name'];
          $cartProDesc = $row['description'];
          $cartSellPrice = $row['sell_price'];
          $cartQty = $row['quantityCart'];

          $compute = $cartSellPrice * $cartQty;
          $totalAmount += number_format((float)$compute, 2, '.', '');
       ?>

Upvotes: 2

Kisaragi
Kisaragi

Reputation: 2218

Set $total_amount = 0 above your loop.

Within your loop you add:

$total_amount += number_format((float)$compute, 2, '.', '');

This will ADD to $total_amount.

You are currently resetting the value of total amount with each interaction of your loop.

Upvotes: 0

Related Questions