当用户将商品添加到购物车时,它将保存在我的临时数据库表 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>
计算总价时麻烦添加到购物车并记录在表中,另一个是SQL计算:“select sum('goos_price') * buys_count as total_price from your_table”;if use php :$total_price = 0;foreach ($cart_data as $key=>$val){ $total_price = $val['goos_price'] * $val['buys_count'];}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句