我有一张产品价格模型成本库存表,以便我更轻松地计算每个产品的总客户支付
<?php echo number_format($show['quantity'] * $show['product_price'],0,',','.'); ?>
我需要显示此计算的总和,但正如您所见,它们是在PHP中实时计算的。有办法吗?
这是完整的代码
<?php
$result=mysqli_query($database,"SELECT * FROM `products` order by `category` ASC");
$rows=mysqli_num_rows($result);
if(mysqli_num_rows($result)>0){
?>
<table class="sales">
<tr>
<td>Quantity</td>
<td>Product Cost</td>
<td>Customer Pays</td>
</tr>
<?php if($rows){$i=0;while($show=mysqli_fetch_assoc($result)){?>
<tr>
<td><?php echo number_format($show['quantity'],0,',','.'); ?></td>
<td><?php echo number_format($show['product_cost'],0,',','.'); ?></td>
<td><?php echo number_format($show['quantity'] * $show['product_cost'],0,',','.'); ?></td>
</tr>
<?php }}?>
</table>
TOTAL CUSTOMER PAY FOR ALL PRODUCTS = EXAMPLE $10.234
如果结果中有5种不同的产品具有不同的价格和不同的客户支付,我需要总结所有这些客户支付并在此处显示
<?php }else{?>
No products to show
<?php }?>
编辑:已解决
解决方案是
<?php
$count = mysqli_query($database, "SELECT SUM(stock * cost) AS totalPaid
FROM products");
while($total = mysqli_fetch_assoc($count)){
echo number_format($total['totalPaid'],0,',','.');} ?>
感谢McAdam331的正确答案
答案 0 :(得分:0)
您可以将SUM
添加为SQL查询的一部分:
SELECT SUM(quantity * product_cost) AS totalPaid
FROM myTable
GROUP BY customer;
我只是猜测你根据问题中的线索对客户进行分组,但你可以根据需要改变它。