gpt4 book ai didi

php - 动态计算php mysql结果总和

转载 作者:行者123 更新时间:2023-11-28 07:37:59 25 4
gpt4 key购买 nike

我有一张包含产品价格模型成本库存等的表格,这样我可以更轻松地计算客户为每个产品支付的总费用

<?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的正确回答

最佳答案

您可以将 SUM 添加为 SQL 查询的一部分:

SELECT SUM(quantity * product_cost) AS totalPaid
FROM myTable
GROUP BY customer;

我只是猜测您会根据问题中的线索对客户进行分组,但您可以将其更改为您需要的任何内容。

关于php - 动态计算php mysql结果总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31124551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com