Forum Moderators: coopster

Message Too Old, No Replies

debits, credits and balance

how do I show the balance?

         

mooger35

9:33 pm on Feb 28, 2006 (gmt 0)

10+ Year Member



On my site I have a password protected finances page. Basically shows debits, credits and balance with the date and a description of each transaction.

What I would like to do is have this page done via the database route.

Now I have the mysql db set up as follows

transid ¦ date ¦ description ¦ deposits ¦ withdrawals ¦

The problem I have is in showing the running balance after every transaction. What should happen is in every row it should total the deposit of that row plus the previous rows, minus the withdrawals from all previous rows as well. How would I set that up in the php code?

whoisgregg

10:27 pm on Feb 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How do you have the loop setup now?

If you have a loop construct like this:

for ( $r=0; $r<$count_lines; $r++ ) {

Then you should be able to do similar to this pseudocode:

$running_balance = $lines[[red]$r-1[/red]] + $lines[[red]$r[/red]];

mooger35

10:35 pm on Feb 28, 2006 (gmt 0)

10+ Year Member



currently I have it set up like this (minus the table coding:

$balance = 0;
while ($row_finance = mysql_fetch_assoc($finance)){
$balance .= $balance + $row_finance[deposit] - $row_finance[withdrawal];
echo $row_finance[date]." ¦ ".$row_finance[description]." ¦ ".$row_finance[deposit]." ¦ ".$row_finance[withdrawal]." ¦ ".$balance;
}

but that doesn't work as it isn't doing the addition/substration. It just adds it on.

mooger35

10:44 pm on Feb 28, 2006 (gmt 0)

10+ Year Member



Got it.

I just have to take out the "."

$balance = $balance + $row_finance[deposit] - $row_finance[withdrawal];