循环启动时的Php sum值

时间:2017-04-14 10:51:50

标签: php phpquery

嗨我在变量中有一个值我想第一次在循环启动时添加此值。例如

<?php

   $balance = 100;
   while($row = mysql_fetch_array($query)
   {
      echo $row['amount'] + $balance; // for example $row['amount'] is 50, 20 , 30;
   }
 ?>

我想要结果如下

<?php       

   150
   170
   200

?>

2 个答案:

答案 0 :(得分:0)

最简单的方法

<?php

   $balance = 100;
   while($row = mysql_fetch_array($query)
   {
      $balance = $balance + $row['amount'];
      echo $balance;
   }
 ?>

答案 1 :(得分:0)

示例代码..

$balance = 100;
$counter=1;
$total=0;
while($row = mysql_fetch_array($query)
{
    if($counter==1)
    {
        $total=$row['amount'] + $balance; // for example $row['amount'] is 50, 20 , 30;
    }
    else
    {
        $total = $row['amount']+$total;
    }

    echo $total.'<br>';

    $counter++;
}