我列出了学生的费用详情,而且相同的代码是,
<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>
将输出显示为
此处每个小组都有三种不同类型的费用,我只需要在一个小组下显示这些类型的总和,
Fee Group Fee Amount Action
Ist Term - LKG 6000 Pay
IInd Term - LKG 4500 Pay
以上是所需的输出..
我在foreach循环中给出了以下内容,
if ($group_id !== $finalGroupId) {
$group_total += $fee_value->amount;
echo $group_total;
}
如果通过迭代的费用组ID与fee_value-&gt; fee_groups_id不匹配,则计算值的总和,但不起作用..
请帮助我在foreach循环中获得所需的输出..
var_export($student_due_fee);
给出以下结果,
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', )), ), )), )
答案 0 :(得分:1)
你将这个分配到很晚
$finalGroupId = $group_id;
你在循环中检查它,但它在那里是未定义的。
我不知道你打算做什么,或者我会帮助你。进一步使用严格类型检查!==
进行检查的方式,组ID永远不会等于最终组,因此无论如何都会添加它们。
就个人而言,我会制作一个阵列。
$totals = [];
而不是检查这个
if ($group_id !== $finalGroupId) {
$group_total += $fee_value->amount;
echo $group_total;
}
我只需在数组中添加一个键(数组键是唯一的)
$totals[$group_id] = $fee_value->amount;
然后循环后再做
$total = array_sum($totals);
<强>更新强>
你说:
@ArtisticPhoenix,如果组ID不同,我们可以将该特定组ID的总量相加,这样我就可以制作该代码。但我知道这是错误的方法,这就是我寻求帮助的原因。
我回答说:
如果它不同,你如何总结那个特定的组ID,这真的没有意义。你可以总结一堆相同的组ID,但是当它不同时,你不能总结一个特定的组ID,因为它与什么不同。
如果你想像组ID那样求和,你可以检查你是否已经在数组中设置它,然后你可以添加它。
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;
}
正如我所说,你不能总结一个不同的东西,因为它与什么不同。
如果你的意思是它与其他所有东西不同,那么你就无法总结它,因为它以前从未见过。这意味着你有一个值,你不能总和一个值。
如果你的意思与当前的那个不同,那么每当你找到一个与当前的不同的时候,你就会恢复其他的一切,这在大多数情况下是浪费时间。
因此,将前一个与前一个相加更容易,如果这就是你的意思,那么这个更新就是你想要的。
如果你的意思是只计算唯一的组ID,那么在此更新之前我已经告诉过你。因为数组键是唯一的,所以$totals
数组只能包含每个项的一个值,它是presumably
的最后一个值,与具有相同id的另一个值相同。然后,总数数组包含每个唯一ID的值,然后我们总和(或不是,取决于您真正想要做的令人困惑的事情)。