我在这里得到了这个PHP代码......
$totalBalance = 0;
foreach($resultArray as $row) {
foreach($row as $key =>$field) {
echo $field['remaining'] . "<br>";
}
}
$ resultArray是从我的SQL查询返回到数组的内容。
天平colunm的前几个记录是..
389.96 6433.66 52.52 107.25
然而,当我运行这个PHP代码时,我得到了
1 五 1 3
为什么要这样做,我该如何解决?
如果有帮助,这是SQL查询
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
我做了一个$ resultArray的print_r并得到了这个......
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 )
答案 0 :(得分:1)
你正在做许多人:
$totalBalance = 0;
foreach($resultArray as $row) {
echo $row['remaining'] . "<br>";
}
如果你想计算总平衡:
$totalBalance = 0;
foreach($resultArray as $row) {
echo $row['remaining'] . "<br>";
$totalBalance += $row['remaining'] ;
}
echo $totalBalance . "<br>";
答案 1 :(得分:0)
<?php
$results = array( array('balance'=>123.45), array('balance'=>32.12));
$total = 0;
foreach( $results as $row )
{
$total+=$row['balance'];
}
echo $total;
?>
输出:
155.57
答案 2 :(得分:0)
不应该是:
$totalBalance = 0;
foreach($resultArray as $row) {
foreach($row as $key =>$field) {
echo $field['remaining'] . "<br>";
}
}
而不是:
$totalBalance = 0;
foreach($resultArray as $row) {
foreach($row as $key =>$field) {
echo $field['balance'] . "<br>";
}
}