douman6245 2015-01-02 21:55
浏览 56

在codeigniter中使用赋值运算符时结果错误

I'm having trouble when calculate grand total result from database mysql. I use codeigneter and newbei.

Here is my db:

selldate | sell
--------------
 9 2014  |  20
10 2014  |  25    
11 2014  |  15
$n2 = -1;

$selling = $this->db->query("select sell as Y , date_format((selldate),'%M %Y') as month  from selling where id='$id' and selldate between '$dt1' and '$dt2'");

foreach ($selling->result() as $row){
    $u = $n2++;
    $t = $u*2+1;
    $yt = $row->Y * $t;
    $t2 = $t*$t;
    $r = 0;
    $r += $yt;
    $w = 0;
    $w += $t2;
    ?>
    <tr>
    <td><?php echo $row->month;?></td>
    <td><?php echo $row->Y;?></td>
    <td><?php echo $t;?></td>
    <td><?php echo $yt;?></td>
    <td><?php echo $t2;?></td>
    </tr>
    <?php 

    }; ?>
    <tr>
    <td></td>
    <td>Grand Total</td>
    <td></td>
    <td><?php echo "Grand Total is $r";?></td>
    <td><?php echo "Grand Total is $w";?></td>
    </tr>
    </table>

The problem is in $r += $yt; and $w +=$t2; it calculates the grand total for the table.

When i used $r=0; and $w=0;, then i get wrong result for grand total, but when i delete $r=0; and $w=0; just use $r+=$yt only i get true result for grand total, but there have a error message

Severity: Notice Message: Undefined variable: r

  • 写回答

1条回答 默认 最新

  • duanmengsuo9302 2015-01-03 04:22
    关注

    Like Sean has said you have to put $r=0 and $w=0 outside for-loop and next to $n2 = -1.

    Currently what happens is each time for-loop iterates $r and $w becomes 0. Hence you get a wrong answer. That is the problem. Once you have declared $r and $w as 0 outside then okay.

    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致