I got this PHP code here…
$totalBalance = 0;
foreach($resultArray as $row) {
foreach($row as $key =>$field) {
echo $field['remaining'] . "<br>";
}
}
$resultArray is what is returned from my SQL query into an array.
The first few records of the balance colunm are..
389.96
6433.66
52.52
107.25
however when I run this php code I get
1
5
1
3
why is it doing this and how can I fix it?
Here is the SQL query if it helps
SELECT c.clientid, c.clientname, c.billingdate,
case when (select ifnull(sum(total), 0) from invoice i
where i.client = c.clientid and i.isdeleted = 0) - (select ifnull(sum(p.amount), 0) from payment p inner join invoice i on p.invoice = i.invoiceid
where i.client = c.clientid and i.isdeleted = 0) < 0 then (select ii.total from invoice ii where ii.client = c.clientid and ii.isdeleted=0 order by ii.invoiceid desc limit 1) else (select ifnull(sum(total), 0) from invoice i
where i.client = c.clientid and i.isdeleted = 0) - (select ifnull(sum(p.amount), 0) from payment p inner join invoice i on p.invoice = i.invoiceid
where i.client = c.clientid and i.isdeleted = 0) end as remaining,
case c.isactive+0
when '1' then 'Stop'
else 'Start'
end as Active
FROM client c
ORDER BY clientname
I did a print_r of $resultArray and got this…
Array ( [0] => Array ( [clientid] => 1 [clientname] => client A [billingdate] => 14 [remaining] => 389.96 [active] => Stop ) [1] => Array ( [clientid] => 178 [clientname] => client B [billingdate] => 23 [remaining] => 6433.66 [active] => Stop ) [2] => Array ( [clientid] => 3 [clientname] => client C [billingdate] => 19 [remaining] => 52.52 [active] => Stop ) [3] => Array ( [clientid] => 105 [clientname] => client D [billingdate] => 23 [remaining] => 107.25 [active] => Start )
You are doing 1 foreach to many:
And if you want to calculate the totalbalance: