2017-04-01 183 views
0

當用戶將物品添加到購物車時,它將保存在我的臨時數據庫表user_cart中,並且會在購物車頁面中顯示物品的輸出,用戶會在他們的購物車中看到他們的物品所以我使用的PHP代碼是while循環來輸出項目。如何計算所有項目的價格並將其輸出到總計部分?如何計算購物車中物品的動態價格並輸出總計

<table class="table table-hover"> 
    <thead> 
     <tr> 
      <th>Product</th> 
      <th>Quantity</th> 
      <th class="text-center">Price</th> 
      <th class="text-center">Total</th> 
      <th> </th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
     <?php 
      $count = 1; 
      $cart_query = "Select * from user_cart where email = '$email'"; 
      $cart_res = mysqli_query($con,$cart_query); 
      while($cart_row = mysqli_fetch_assoc($cart_res)) { 
     ?> 
      <td class="col-sm-8 col-md-6"> 
      <div class="media"> 
       <a class="thumbnail pull-left" href="#"> <?php 
       $image_count = 1; 
       $product_name = stripslashes($cart_row["product_name"]); 
       $image_query = "Select * from tbl_products where item_category = 'Laptop' AND item_name = '$product_name'"; 
       $image_res = mysqli_query($con,$image_query); 
       while($row = mysqli_fetch_assoc($image_res)){ 
       $imageData = $row["images"]; 
       echo '<img src ="data:image/jpeg;base64,'.base64_encode($imageData).'"class="img-responsive" style = "width:100px; height:80px;"/>';?> 
       <?php $count++; } ?> 
       </a> 
       <div class="media-body"> 
       <h4 class="media-heading"><a href="#"><?php echo stripslashes($cart_row["product_name"]);?></a></h4> 
       <h5 class="media-heading"> by <a href="#"><?php echo $cart_row["brand"];?></a></h5> 
       <span>Status: </span> 
       <span class="text-success"> 
       <strong><?php echo $cart_row["item_available"];?></strong> 
       </span> 
       </div> 
      </div></td> 
      <td class="col-sm-1 col-md-1" style="text-align: center"> 
       <input type="email" class="form-control" id="exampleInputEmail1" value="<?php echo $cart_row["quantity"];?>"> 
      </td> 
      <td class="col-sm-1 col-md-1 text-center"> 
      <strong><?php echo $cart_row["item_price"];?></strong></td> 
      <td class="col-sm-1 col-md-1 text-center" id = "item_total_price"> 
      <strong><?php echo $cart_row["item_total_price"];?></strong></td> 
      <td class="col-sm-1 col-md-1"> 
      <form action = "functions/delete.php" method = "post"> 
      <button type="submit" class="btn btn-danger"> 
      <input type = "hidden" value = "<?php echo $cart_row['cart_id'];?>" name = "cart_id"> 
      <span class="glyphicon glyphicon-remove"></span> Remove 
      </button></td> 
      </form> 
      </tr> 
      <?php $count++; } ?> 
      <tr> 
      <td>   </td> 
      <td>   </td> 
      <td>   </td> 
      <td><h5>Subtotal</h5></td> 
      <td class="text-right" id = "sub_total"> 
      <h5><strong></strong></h5></td> 
      </tr> 
      <tr> 
      <td>   </td> 
      <td>   </td> 
      <td>   </td> 
      <td><h3>Total</h3></td> 
      <td class="text-right"> 
       <h3><strong>$31.53</strong></h3> 
      </td></tr> 
      <tr> 
      <td>   </td> 
      <td>   </td> 
      <td>   </td> 
      <td> 
      <button type="button" class="btn btn-default"> 
      <span class="glyphicon glyphicon-shopping-cart"> 
      </span> Continue Shopping</button></td> 
      <td> 
      <button type="button" class="btn btn-success"> 
       Checkout <span class="glyphicon glyphicon-play"></span> 
      </button></td> 
      </tr> 
      </tbody> 
      </table> 

回答

0

不厭其煩計算總價並記錄在一個表中時添加到購物車,另一種是SQL的計算:「SELECT SUM(‘goos_price’)* buys_count從your_table TOTAL_PRICE」;如果使用PHP :$ total_price = 0; foreach($ cart_data as $ key => $ val){$ total_price = $ val ['goos_price'] * $ val ['buys_count'];}

+0

我的英語不是很好,對不起 –

+0

謝謝你好吧我也不好用英語 –

+0

然後:) :) –

相關問題