Jacky
Jacky

Reputation: 781

How to calculate the total sum end of each loop?

I am fetching the data from the table "daybook" according to date.I haved fetched data and display in the front-end also.

My problem is i have two column debit and credit,it displays the amount.I have to calculate the sum of this at each end of looping.

My expected result: enter image description here

My code: My view page code:

<tbody>
<?php
    $totalQuantity = 0;
    $totalBundle = 0;
    $rowcount = 1;
    foreach ($query as $key => $value){
        $counter = 0;
?>
<?php
    foreach ($value as $sub_key => $sub_value) {
        if($counter == 0) {
            if ($sub_value['dc']== "D"){ 
                $totalQuantity  + = $sub_value['Amount'];
            }
?>
        <tr>
            <td><?php echo $sub_value['code'] ?></td>
            <td><?php echo date('d/m/Y', strtotime($sub_value['date1']));?></td>
            <td><?php echo $sub_value['part'] ?></td>
            <td><?php if ($sub_value['dc']== "D"){ echo $sub_value['Amount'];} ?></td>
            <td><?php if ($sub_value['dc']== "C"){ echo $sub_value['Amount'];} ?></td>
            <?php $rowcount +=1?>
        </tr>
<?php
    $counter++;
}else {?>

        <tr>
            <td></td>
            <td></td>
            <td><?php echo $sub_value['part'];?></td>
            <td><?php if ($sub_value['dc']== "D"){ echo $sub_value['Amount'];} ?></td>
            <td><?php if ($sub_value['dc']== "C"){ echo $sub_value['Amount'];} ?></td>
        </tr>   
<?php
            } // else
        } // foreach
    }
    ?> 
    <tr>

        <td></td>
        <td></td>
        <td>Total</td>
        <td><?php echo $totalQuantity; ?></td>

    </tr>
</tbody>

please help me to solve the sum of debit and credit in each one. My output:

enter image description here

Upvotes: 1

Views: 255

Answers (1)

Nimesh Patel
Nimesh Patel

Reputation: 804

Remove Space between "+ ="

$totalQuantity  += $sub_value['Amount'];

Upvotes: 2

Related Questions