2016-08-28 41 views
1

如何獲得購物車的總量並將其放置在桌子下面的總量?我應該使用JavaScript還是隻使用PHP?請給我一些建議。謝謝。如何獲取表格上的總行數?

 <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> 

這是我的桌子。你可以看到,當我在while循環之外回顯$totalamount時,它只能得到最後一行。

回答

2

這將適用於你的情況。

<?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, '.', ''); 
     ?> 
0

你的循環上方設置$total_amount = 0

在你的循環中,您添加:

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

這樣會加重$total_amount

您目前正在使用循環的每次交互重置總金額的值。

0

您可以設置$total_amount = 0和while循環確保您添加增量喜歡$total_amount += $compute總量只要確保你做你的號碼浮法您做出increment.just一個這麼簡單