当用户将项目添加到其购物车时,它将保存在我的临时数据库表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 :(得分:0)
在计算总价格并在表格中记录时麻烦添加到购物车,另一个是SQL计算:“从your_table选择sum('goos_price')* buys_count为total_price”;如果使用php:$ total_price = 0; foreach($ cart_data as $ key =&gt; $ val){$ total_price = $ val ['goos_price'] * $ val ['buys_count'];}