从mysql表中获取while()循环量的累计总量(通过添加)

时间:2012-03-06 19:03:25

标签: php mysql loops while-loop

我的查询等工作正常,但我无法在变量中添加总计结果以回显。

根据我的情况,我在$ full_total中获得0(零)金额;值。

while($row = mysql_fetch_array($all_donations)) {
        $donation_date = date("m/d/Y", $row['date']);
            echo "<tr><td>" . $donation_date ."</td><td>" . number_format($row['price'], 2) ."</td></tr>";

            $total_dontations = number_format($row['price'], 2);
            $full_total += $total_donations;
      }
// echo this amount outside of the loop.
echo $full_total;

3 个答案:

答案 0 :(得分:1)

尝试初始化full_total

$full_total = 0;
while($row = mysql_fetch_array($all_donations)) {
//...

答案 1 :(得分:1)

尝试在$full_total语句上方定义while,如下所示:

$full_total = 0;
while($row = mysql_fetch_array($all_donations))
{ ...

答案 2 :(得分:1)

我认为它与Using a variable outside of the while loop (scope)

类似

根据该帖子,你可以这样做

$full_total = "";
while($row = mysql_fetch_array($all_donations)) {
        $donation_date = date("m/d/Y", $row['date']);
            echo "<tr><td>" . $donation_date ."</td><td>" . number_format($row['price'], 2) ."</td></tr>";

            $total_dontations = number_format($row['price'], 2);
            $full_total += $total_donations;
      }
// echo this amount outside of the loop.
echo $full_total;