I wrote this code to sum only amount but is not working.Everything works perfectly except the sum. Can some one help me out? Here is the code:
<tr >
<td > Total </td>
<td >
<?php $sum2=0; foreach($contributions_ as $val){
$sum = $sum2 + $contributions_[$val];
}echo array_sum($contributions_);
?>
</td>
</tr>
Here is the php:
<?php
$tempArray = array(
$rowRes['amount'],
$rowRes['remarks'],
$rowRes['ID']
);
$contributions_[] = $tempArray;
?>
I want to sum only amount(s) whiles the condition is true
This is my query which worked perfectly but cannot sum amount after displaying the various members contributions .
<?php $selectQuery = "select a.*,b.* ID, monthname(date) as month,month(date) as mnth, year(date) as yr, type,amount, recipient_initials,remarks, in_kind_items,other_type,other_payment_name,date from member_payments a inner join member b on a.memberID = b.ID";
?>
Why can't I sum amount here like SUM(amount)
You used $sum and $sum2 in this line:
$sum = $sum2 + $contributions_[$val];
If I'm reading your code right, it should be more like this:
<?php $sum2=0; foreach($contributions_ as $val){
$sum2 = $sum2 + $contributions_[$val];
}echo $sum2;
?>
Sorry, I don't know what you mean by "I want to sum only amount(s) whiles the condition is true".
<tr >
<td> Total </td>
<td>
<?php
$sum = 0;
foreach($contributions_ as $val){
$sum = $sum + $val['amount'];
}
echo $sum;
?>
</td>
</tr>
<?php
$tempArray = array(
$rowRes['amount'],
$rowRes['remarks'],
$rowRes['ID']
);
$contributions_[] = $tempArray;
$sum += $rowRes['amount'];
?>
Got it now after echoing sum