duanjizi9443 2015-01-29 16:51
浏览 19
已采纳

列计算中第一行为零

I have a sketch of calculation like this

+------------+---------------+-------------+-------------+ 
    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
+------------------------------------------+-------------+ 

The table above is what I am expecting to have. However this is the result what I got for now:

+------------+---------------+-------------+-------------+ 
    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
+------------------------------------------+-------------+ 

The problem is it give the 0 saldo in the first row even though the calculation is correct.

Here is my full code

    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>";
            }

Thanks in advance for the help.

  • 写回答

1条回答 默认 最新

  • dongxie9448 2015-01-29 16:58
    关注

    You're using a variable before you calculate its value.

    For example, lets look at these two lines:

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

    You're using the value of $totalEight to calculate $sourceCash, then you update the value of $totalEight. On the first iteration, the value of all of those columns will be zero, which is why you're displaying zero.

    You probably meant to reverse the order of those two lines.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗