我目前正在制作一个非常标准的电子商务购物车,我已将其设置为显示在购物车中的商品从我的数据库循环到一个表格。
table class="large-16" style="margin-top: 20px;">
<tr>
<th>Action:</th>
<th>Product:</th>
<th>Price:</th>
<th>Size:</th>
<th>Color:</th>
<th>Quantity:</th>
<th>Price Total:</th>
</tr>
<?php while ($row = mysqli_fetch_array($result)) { ?>
<tr>
<td>
<a href="delete-product-handler.php?id=<?php
echo $row['product_id']; ?>" onclick="return confirm("Are you sure you want to remove this product from your shopping cart?")">
<img src="img/delete.png" alt="delete button">Remove</a>
</td>
<td><?php echo $row['product_name']; ?></td>
<td><?php echo $row['product_price']; ?></td>
<td><?php echo $row['product_size']; ?></td>
<td><?php echo $row['product_color']; ?></td>
<td><?php echo $row['product_quantity']; ?></td>
<td><?php $totalItemPrice[] = ($row['product_price'] * $row['product_quantity']);
echo $totalItemPrice; ?></td>
</tr>
正如您所看到的那样,它设置为将每件商品的价格加到我想购买的相同商品的数量上。
我现在如何将所有总计价格的总和相加,以获得我购买的所有商品的全部价格?
答案 0 :(得分:1)
答案 1 :(得分:0)
替换
td><?php $totalItemPrice[] = $row['product_price']*$row['product_quantity']); echo $totalItemPrice; ?></td>
由此
<td><?php echo $row['product_price']*$row['product_quantity']; ?></td>
答案 2 :(得分:0)
我终于明白了,感谢所有帮助过的人。
以下是使用的编码:
<?php
$total = 0;
while ($row = mysqli_fetch_array($result)) { ?>
<tr>
<td><a href="delete-product-handler.phpid=>?php ?>" onclick="return confirm("Are you sure you want to remove this product from your shopping cart?")"><img src="img/delete.png" alt="delete button">Remove</a></td>
<td><?php echo $row['product_name']; ?></td>
<td><?php echo $row['product_price']; ?></td>
<td><?php echo $row['product_size']; ?></td>
<td><?php echo $row['product_color']; ?></td>
<td><?php echo $row['product_quantity']; ?></td>
<td><?php echo $row['product_price'] * $row['product_quantity'];
$total += $row['product_price'] * $row['product_quantity']; ?></td>
</tr>
<?php } ?>
<tr><td colspan="6"></td>
<td>Total: <?php echo $total; ?></td></tr>
干杯。