Hello all,
I have this query:
SELECT select_month
, SUM( IF( cat_id = "1", budget_amount, 0 ) ) AS "TOTAL INCOME"
, SUM( IF( cat_id != "1", budget_amount, 0 ) ) AS "TOTAL EXPENSE"
, SUM( IF( cat_id = "1", budget_amount, 0 ) ) - SUM( IF( cat_id != "1", budget_amount, 0 ) ) AS "NET INCOME"
, SUM( IF( cat_id = "1", budget_amount, 0 ) ) - SUM( IF( cat_id != "1", budget_amount, 0 ) ) + start_balance AS "END BALANCE"
FROM bdgt_startbalance, bdgtbymonth INNER JOIN bdgtcategoryitems
USING ( item_id )INNER JOIN bdgt_month_year_combo
USING ( monthly_budget_id )
GROUP BY select_month
which produces this result set:
select_month TOTAL INCOME TOTAL EXPENSE NET INCOME END BALANCE
2010-01-01 3605 1901 1704 3440
2010-02-01 3050 1637 1413 3149
2010-03-01 1850 1812 38 1774
2010-04-01 250 1812 -1562 174
2010-05-01 250 1812 -1562 174
I am trying to display the results in a way something like this:
select_month 2010-01-01 2010-02-01 2010-03-01 2010-04-01 2010-05-01
TOTAL INCOME 3605 3050 1850 250 250
TOTAL EXPENSE 1901 1637 1812 1812 1812
NET INCOME 1704 1413 38 -1562 -1562
END BALANCE 3440 3149 1774 174 17
basically swapp the horizontal access with the vertical.
So I built this foreach loop to try to store in arrays the values so that I could echo out a HTML table but for some reason I am only getting the last 2 values??
here is the code:
$frec = '';
foreach($totals as $total){
if($total['select_month'] != $frec){
$tia = array($total['TOTAL INCOME']);
$tea = array($total['TOTAL EXPENSE']);
$nia = array($total['NET INCOME']);
$eba = array($total['END BALANCE']);
$frec = $total['select_month'];
}else{
array_push($tia, $total['TOTAL INCOME']);
array_push($tea, $total['TOTAL EXPENSE']);
array_push($nia, $total['NET INCOME']);
array_push($eba, $total['END BALANCE']);
}
}
print_r($tia);
Any ideas??