gpt4 book ai didi

php - 遍历 while 循环并将数字相加

转载 作者:行者123 更新时间:2023-11-29 11:09:23 25 4
gpt4 key购买 nike

我从数据库获取数据并根据 billing_type 和日期执行计算。我能够正确循环并执行每个日期的计算。不过,我现在正在尝试将计算结果相加。

例如,如果计算结果相等:10、7、30、28

我希望能够将它们相加并显示 75 的结果

这是我的代码:

$sql = "SELECT billing_amount, billed_by, open, SUM(tickets), COUNT(casekey)
FROM pcnclientreport
WHERE open = '$date' GROUP BY '$date'";

$res = mysqli_query($conn, $sql);

while($row = mysqli_fetch_assoc($res) ){
if($row['billed_by'] == 'Month'){
$gsliability = $row['billing_amount'];
}
else if ($row['billed_by'] == 'Case'){
$gsliability = ($row['billing_amount'] * $row['COUNT(casekey)']);
}
else if ($row['billed_by'] == 'Ticket'){
$gsliability = ($row['billing_amount'] * $row['SUM(tickets)']);
}
}

echo $gsliability

最佳答案

$sql = "SELECT billing_amount, billed_by, open, SUM(tickets), COUNT(casekey)
FROM pcnclientreport
WHERE open = '$date' GROUP BY '$date'";

$res = mysqli_query($conn, $sql);

$sum = 0;
while($row = mysqli_fetch_assoc($res) ){

$gsliability = 0;

if($row['billed_by'] == 'Month'){
$gsliability = $row['billing_amount'];
}
else if ($row['billed_by'] == 'Case'){
$gsliability = ($row['billing_amount'] * $row['COUNT(casekey)']);
}
else if ($row['billed_by'] == 'Ticket'){
$gsliability = ($row['billing_amount'] * $row['SUM(tickets)']);
}

$sum += $gsliability;
}

echo $sum;

关于php - 遍历 while 循环并将数字相加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40871044/

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