2016-08-28 2 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에 추가됩니다

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

.

현재 루프의 상호 작용마다 총 금액의 값을 재설정하고 있습니다.

0

당신은 $total_amount = 0을 설정하고 while 루프에 해당

같은 간단한 increment.just을하기 전에 당신은 당신이 전화 번호 부유물을 확인 $total_amount += $compute 같은 총량에 증분을 추가해야 할 수 있습니다
관련 문제