1

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

12
  • please add what you are getting and what you are expecting... Commented Jan 5, 2018 at 10:28
  • Move summing to the 1st loop Commented Jan 5, 2018 at 10:29
  • @YashParekh expected : -2.272.987.032, what I've got : -241.839.381.541. Commented Jan 5, 2018 at 10:33
  • Maybe if you showed us a relevant example of the data in $arr_amt some of this code would make more sense Commented Jan 5, 2018 at 10:36
  • @splash58 I've already did, but I got this : -244,112,368,573, my expectation is : -2.272.987.032 Commented Jan 5, 2018 at 10:36

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for ur help, my problem solved. The first row already correct, but the next row it return a big number. I'll try to catch it up, once again thanks Simos :))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.