列计算中第一行为零

时间:2015-01-29 16:51:31

标签: php

我有一个像这样的计算草图

+------------+---------------+-------------+-------------+ 
    Name          IN              OUT          SALDO
+------------+---------------+-------------+-------------+ 
   Cash 8%        $50              -            $50
   Cash 4%        $20              -            $70
   Cash 12%       $70              -            $140
   Claimed         -              $60           $80
   Claimed         -              $20           $60
+------------------------------------------+-------------+ 
               TOTAL SALDO                      $60
+------------------------------------------+-------------+ 

上面的表格是我期待的。然而,这就是我现在得到的结果:

+------------+---------------+-------------+-------------+ 
    Name          IN              OUT          SALDO
+------------+---------------+-------------+-------------+ 
   Cash 8%        $50              -            0
   Cash 4%        $20              -            $50
   Cash 12%       $70              -            $70
   Claimed         -              $60           $140
   Claimed         -              $20           $80
+------------------------------------------+-------------+ 
               TOTAL SALDO                      $60
+------------------------------------------+-------------+ 

问题是即使计算正确,它也会在第一行给出0 saldo。

这是我的完整代码

    if ($saldo->num_rows > 0) {

        while($objek = $saldo->fetch_object()) {
            $i++;
            if ($objek->eight_percent) 
            {
            $sourceCash = $totalTwelve + $totalEight + $totalFour + $totalTwo - $totalClaim - $totalCashout;
            $totalEight = $totalEight + $objek->eight_percent;
                echo "<tr>
                        <td>Cashback 8%</td>
                        <td> - </td>
                        <td>".$sourceCash."</td>
                     </tr>";
            }

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您在计算其值之前使用变量。

例如,让我们看看这两行:

$sourceCash = $totalTwelve + $totalEight + $totalFour + $totalTwo - $totalClaim - $totalCashout;
$totalEight = $totalEight + $objek->eight_percent;

您使用$totalEight的值来计算$sourceCash然后更新$totalEight的值。在第一次迭代中,所有这些列的值都将为零,这就是您显示零的原因。

你可能想要颠倒这两行的顺序。