【问题标题】:counting total within a while loop在while循环中计算总数
【发布时间】:2016-06-25 05:30:52
【问题描述】:

在我的项目中,我使用 while 循环从数据库中获取数据,其中每个数据都由作为 $cid 的客户 ID 获取。现在所有的数据都被计算出来并最终放入一个变量 $cashflow 中。现在假设有 20 人,因此该表将显示 20 条记录,其中每个数据都针对每个人/客户进行计数。现在我要做的是计算所有 20 条记录的总数并分别回显。

例如。

Cust.1:500(根据 $cashflow 计算)
客户。 2 : 1000
Cust.3 : 800 .....等等。

现在这些数据在我的表格中得到了很好的体现。现在我想要的是分别添加 500+1000+800 和 echo 2300。但我无法弄清楚如何在我的代码中做到这一点。我应该将循环中的变量作为 ++ 计数器吗?或者是其他东西?请帮忙。

代码:

$cash = "SELECT SUM(paid) FROM hbil WHERE cid = '".$cid."' AND method = 'cash'";
$cash_qur = mysql_query($cash) or die(mysql_error());

$cash_neg = "SELECT SUM(price) FROM hbil WHERE cid = '".$cid."' AND method = 'cash' AND price < 0";
$cash_qur_neg = mysql_query($cash_neg) or die(mysql_error());
while($cash_fetch = mysql_fetch_array($cash_qur))
{
    $cash_fet = mysql_fetch_array($cash_qur_neg);
    $cashpay = $cash_fetch['SUM(paid)']; 
    $cashneg = $cash_fet['SUM(price)']; 
    $cash_total = $cashpay-$cashneg; 
    $cashflow = number_format($cash_total,2,'.',',');   
}

【问题讨论】:

标签: php html mysql phpmyadmin


【解决方案1】:
$total = 0;
while (...) {
//...
$total += $cash_total;
}

echo $total;

会成功的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    • 2012-03-23
    • 2013-02-27
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多