I have a query that is supposed to show deposits, withdrawls, and balance. I am summing the deposits and withdrawls to get the balance.
The query and display works ok when ORDER BY
date ASC
.
However, I wish to order by DESC
and this is causing issues with the display of balance. Here is an example.
Date Deposit Withdrawl Balance
Jan 2 $2000 -$2000
Jan 1 $5000 $3000
As you can see I would want the Jan 1 balance to be 5000 and the Jan 2 balance to be 3000. I'm stuck on a good way to solve this. At first, I simply created a column for balance and inserted the balance as the row was being written. But this caused problems when a single row was updated.
Any help appreciated! Thanks.
Here is the code I'm working with.
$result2 = mysql_query("SELECT * FROM bankaccount_transaction WHERE bankaccount_id = '$id' ORDER BY ID DESC ".$pages->get_limit()) or die(mysql_error());
while($row2 = mysql_fetch_array($result2)) {
$date = date('m-d-Y', strtotime($row2['date']));
if ($row2['deposit'] > 0){
$deposit = $row2['deposit'];
$newdeposit = number_format($deposit,2);
$newtotal += $deposit;
}else{
$newdeposit = '';
}
if ($row2['withdrawl'] > 0){
$withdrawl = $row2['withdrawl'];
$newwithdrawl = number_format($withdrawl,2);
$newtotal -= $withdrawl;
}else{
$newwithdrawl = '';
}
display table
}