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