Home How to sum the values in a group in php?
Reply: 0

How to sum the values in a group in php?

user7561
1#
user7561 Published in July 21, 2018, 1:38 pm

I am listing out the fee details of a student and the code for the same is,

    <table class="table table-hover">
      <thead>
        <tr>
          <th>Fees Group</th>
          <th>Fees Type</th>
          <th>Fees Amount</th>
          <th>Action</th>
        </tr>
      </thead>
      <tbody>
        <?php
        $total_amount = "0";
        $total_deposite_amount = "0";
        $total_fine_amount = "0";
        $total_discount_amount = "0";
        $total_balance_amount = "0";
        $group_total = 0;
        $group_id = array();

        foreach ($student_due_fee as $key => $fee) {

          foreach ($fee->fees as $fee_key => $fee_value) {
            $fee_paid = 0;
            $fee_discount = 0;
            $fee_fine = 0;
            $alot_fee_discount = 0;
            $group_id = $fee_value->fee_groups_id;


            if ($group_id !== $finalGroupId) {
              $group_total += $fee_value->amount;
              echo $group_total;
            }

            if (!empty($fee_value->amount_detail)) {
              $fee_deposits = json_decode(($fee_value->amount_detail));

              foreach ($fee_deposits as $fee_deposits_key => $fee_deposits_value) {
                $fee_paid = $fee_paid + $fee_deposits_value->amount;
                $fee_discount = $fee_discount + $fee_deposits_value->amount_discount;
                $fee_fine = $fee_fine + $fee_deposits_value->amount_fine;
              }
            }
            $total_amount = $total_amount + $fee_value->amount;
            $total_discount_amount = $total_discount_amount + $fee_discount;
            $total_deposite_amount = $total_deposite_amount + $fee_paid;
            $total_fine_amount = $total_fine_amount + $fee_fine;
            $feetype_balance = $fee_value->amount - ($fee_paid + $fee_discount);
            $total_balance_amount = $total_balance_amount + $feetype_balance;
            ?>
            <tr>
              <td><?php echo $fee_value->name ?></td>
              <td><?php echo $fee_value->type ?></td>
              <td><?php echo $fee_value->amount ?></td>
              <td>
                <div class="btn-group">
                  <?php
                  if ($feetype_balance > 0) {
                    ?>
                    <a href="<?php echo base_url() . 'payment/paytm/' . $fee->id . "/" . $fee_value->fee_groups_feetype_id . "/" . $_SESSION['studentDetails'][0]->id ?>" class="btn btn-xs btn-primary pull-right myCollectFeeBtn"> Pay </a>

                    <?php
                  } else { ?>
                    <h4 class="label label-success"> Paid </h4>
                  <?php }
                  ?>
                </div>
              </td>
            </tr>

          <?php }
        }
        $finalGroupId =  $group_id;
        ?>
      </tbody>
    </table>

Which displays the output as,

And here each group has three different type of fees and i need to display only the sum of those types under a group like,

Fee Group           Fee Amount         Action
Ist Term - LKG      6000               Pay
IInd Term - LKG     4500               Pay

The above is the desired output..

I have given like the following inside foreach loop,

if ($group_id !== $finalGroupId) {
              $group_total += $fee_value->amount;
              echo $group_total;
     }

If the fee group id through the iteration doesn't match with the fee_value->fee_groups_id, then make the sum of values, but its not working..

Kindly help me to get the desired output inside foreach loop..

var_export($student_due_fee); gives the following result,

array ( 0 => stdClass::__set_state(array( 'id' => '19', 'student_session_id' => '278', 'fee_session_group_id' => '15', 'is_active' => 'no', 'created_at' => '2018-04-17 05:18:41', 'name' => 'Ist Term - LKG', 'fees' => array ( 0 => stdClass::__set_state(array( 'id' => '19', 'student_session_id' => '278', 'fee_session_group_id' => '15', 'is_active' => 'no', 'created_at' => '2018-04-17 05:18:41', 'fee_groups_feetype_id' => '74', 'amount' => '1000.00', 'due_date' => '2018-04-24', 'fee_groups_id' => '69', 'name' => 'Ist Term - LKG', 'feetype_id' => '1', 'code' => 'ISEMTUTION', 'type' => 'I SEM TUTION FEES', 'student_fees_deposite_id' => '0', 'amount_detail' => '0', )), 1 => stdClass::__set_state(array( 'id' => '19', 'student_session_id' => '278', 'fee_session_group_id' => '15', 'is_active' => 'no', 'created_at' => '2018-04-17 05:18:41', 'fee_groups_feetype_id' => '75', 'amount' => '2000.00', 'due_date' => '2018-04-24', 'fee_groups_id' => '69', 'name' => 'Ist Term - LKG', 'feetype_id' => '2', 'code' => 'IITUTION', 'type' => 'II SEM TUTION FEES', 'student_fees_deposite_id' => '0', 'amount_detail' => '0', )), 2 => stdClass::__set_state(array( 'id' => '19', 'student_session_id' => '278', 'fee_session_group_id' => '15', 'is_active' => 'no', 'created_at' => '2018-04-17 05:18:41', 'fee_groups_feetype_id' => '76', 'amount' => '3000.00', 'due_date' => '2018-04-24', 'fee_groups_id' => '69', 'name' => 'Ist Term - LKG', 'feetype_id' => '3', 'code' => 'SPORTS', 'type' => 'SPORTS FEES', 'student_fees_deposite_id' => '0', 'amount_detail' => '0', )), ), )), 1 => stdClass::__set_state(array( 'id' => '65', 'student_session_id' => '278', 'fee_session_group_id' => '16', 'is_active' => 'no', 'created_at' => '2018-04-17 05:20:20', 'name' => 'IInd Term - LKG', 'fees' => array ( 0 => stdClass::__set_state(array( 'id' => '65', 'student_session_id' => '278', 'fee_session_group_id' => '16', 'is_active' => 'no', 'created_at' => '2018-04-17 05:20:20', 'fee_groups_feetype_id' => '77', 'amount' => '2000.00', 'due_date' => '2018-04-24', 'fee_groups_id' => '70', 'name' => 'IInd Term - LKG', 'feetype_id' => '7', 'code' => 'MAGAZINE', 'type' => 'MAGAZINE, PHOTOS,BAG, ID etc ', 'student_fees_deposite_id' => '0', 'amount_detail' => '0', )), 1 => stdClass::__set_state(array( 'id' => '65', 'student_session_id' => '278', 'fee_session_group_id' => '16', 'is_active' => 'no', 'created_at' => '2018-04-17 05:20:20', 'fee_groups_feetype_id' => '78', 'amount' => '1500.00', 'due_date' => '2018-04-24', 'fee_groups_id' => '70', 'name' => 'IInd Term - LKG', 'feetype_id' => '8', 'code' => 'SCHOLASTICS', 'type' => 'CO - SCHOLASTICS', 'student_fees_deposite_id' => '0', 'amount_detail' => '0', )), 2 => stdClass::__set_state(array( 'id' => '65', 'student_session_id' => '278', 'fee_session_group_id' => '16', 'is_active' => 'no', 'created_at' => '2018-04-17 05:20:20', 'fee_groups_feetype_id' => '79', 'amount' => '1000.00', 'due_date' => '2018-04-24', 'fee_groups_id' => '70', 'name' => 'IInd Term - LKG', 'feetype_id' => '9', 'code' => 'BOOKSUNIFORM', 'type' => 'BOOKS, NOTEBOOKS, UNIFORM', 'student_fees_deposite_id' => '0', 'amount_detail' => '0', )), ), )), )
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.321263 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO