Reputation: 137
Here is my code :
<tr align="right">
<td style="white-space: nowrap" align="left"><?= $dt_left_header ?></td>
<td></td>
<?php
//trx data
for($p=0; $p < count($arr_prd_grp); $p++){
$prd_id = $arr_prd_grp[$p] ;
//print_r($arr_prd_grp[$p]);
if($left_header[$j][1] == 1){
echo '<td></td>';
}else{
echo'
<td>'.number_format($arr_amt[$coa_id][$prd_id], 2,',','.').'</td>
';
}
}
//TOTAL
if($left_header[$j][1] == 1){
echo '<td></td>';
}else{
echo'
<td>'.number_format($amt_tot += $arr_amt[$coa_id][$prd_id], 2,',','.').'</td>
';
}
?>
</tr>
In this case, I want to calculate total of $arr_amt[$coa_id][$prd_id]
. My code already calculate it but the result is not equal with my expectation. Can someone tell me how to make it right? Thanks
Upvotes: 1
Views: 67
Reputation: 1390
Move the sum calculation to the first loop, then show the result in the appropriate place. To made it easier i've made an extra variable $totalAmount;
Also my guess is that you are having another outer loop (maybe for each table row). Your current code did not default the totalAmount to 0, so it was adding all the ammounts of each rows, thats why you resulted in such a big number. We add a default value 0 for each row to help that.
<tr align="right">
<td style="white-space: nowrap" align="left"><?= $dt_left_header ?></td>
<td></td>
<?php
//trx data
$totalAmount = 0; // default it
for($p=0; $p < count($arr_prd_grp); $p++){
$prd_id = $arr_prd_grp[$p] ;
//print_r($arr_prd_grp[$p]);
if($left_header[$j][1] == 1){
echo '<td></td>';
}else{
echo'
<td>'.number_format($arr_amt[$coa_id][$prd_id], 2,',','.').'</td>
';
$totalAmount+=$arr_amt[$coa_id][$prd_id];
}
}
//TOTAL
if($left_header[$j][1] == 1){
echo '<td></td>';
}else{
echo'
<td>'.number_format($totalAmount, 2,',','.').'</td>
';
}
?>
</tr>
Upvotes: 1