1

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,

enter image description here

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', )), ), )), )
Maniraj Murugan
  • 8,868
  • 20
  • 67
  • 116

1 Answers1

1

You're assigning this far far to late

    $finalGroupId =  $group_id;

You check it in the loop, but it's undefined in there.

I have no idea what you intend it to be for, or I would help you. Further with the way you are checking with strict type checking !== the group id will never be equal to the final group and so it will just add them no matter what.

Personally I would make an array.

$totals = [];

And instead of checking this

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

I would simply add the in the array with a key (array keys are unique)

$totals[$group_id] = $fee_value->amount;

Then after the loop just do

$total = array_sum($totals);

UPDATE

You said:

@ArtisticPhoenix, If the group id is different we can sum the total amount of that particular group id, in that way i have made that code. But i know its the wrong way of approach and thats why i am seeking for a help.

And I said in reply:

how can you sum that particular group id it if it is different, that really makes no sense. You can sum a bunch of group ids that are the same, but you cant sum a particular one when it's different, because it's different from what.

If you want to sum like group ids you can check if you have set it in the array already, then you can add to it.

if(!isset($totals[$group_id])){
     //first time add it to array
     $totals[$group_id] = $fee_value->amount;
}else{
     //other times add to existing value (AKA SUM)
     $totals[$group_id] += $fee_value->amount;
}

AS I said you can't sum one that is different, because it's different from what.

If you mean it's different then everything else then you can't sum it because it's never been seen before. Meaning you have one value for it and you cant sum one value.

If you mean it's different then the current one, then you would be resuming everything else every time you find one that is different then the current one, which would be most of the time and therefore a waste of effort.

So it's easier to sum one that is the same as a previous one, which if that is what you mean then this update is what you want.

And if you mean sum only unique group ids, I already showed you that before this update. Because array keys are unique then the $totals array can only contain one value for each item, it's the last value which is presumably the same as the other value with the same id. Ergo the totals array then contains the value for each unique id, which we then sum (or not, depending on which confusing thing you really want to do).

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
  • Op is doing a lot of stuff. I don't think that's needed. I think array_chunk(of 3) will do the job for him. – Alive to die - Anant Apr 17 '18 at 07:22
  • @ArtisticPhoenix, If the group id is different we can sum the total amount of that particular group id, in that way i have made that code. But i know its the wrong way of approach and thats why i am seeking for a help. – Maniraj Murugan Apr 17 '18 at 07:27
  • how can you sum `that particular group id` it if it is different, that really makes no sense. You can sum a bunch of group ids that are the same, but you cant sum a particular one when it's different, because it's different from what. – ArtisticPhoenix Apr 17 '18 at 08:10