Welcome Guest, Not a member yet? Register   Sign In
Need help getting data from MySQL
#1

[eluser]d1a8lo24[/eluser]
Hello everyone i need some help figuring out how to get data in a particular way.

These are the fields that I have in the table from where ill be getting the data.

Fields:

transaction_id
transaction_type
transaction_amount

So basically the transaction type is the one that will tell me if its a purchase, credit or a payment, The transaction amount can have either negative or regular values.

Now I know how to use the sum to get a full total which it works fine.

But I want something like the banks have which is a table in which you have the transaction and next to it you will have a balance like below.
Now I know how to do a loop and output everything except how to do the balance for each row.

ATM deposit - 05/21/2011 - $10.00 - balance - $25.00
Purchase - 05/21/2011 - $5.00 - Balance - $15.00
Deposit - 05/21/2011 - $20.00 - Balance $20.00

Is it possible to get this from 1 table like I have or do i need something else. Or can this be done using in code.

If anyone can help me with an example either in MySQL or in php code I will really appreciated.
#2

[eluser]bgreene[/eluser]
SET @runtot:=0;
SELECT transid,transtype,transamt,(@runtot := @runtot + transamt) AS acntbal FROM
(SELECT transid,transtype,transamt FROM transtable ORDER BY transid) AS x
#3

[eluser]d1a8lo24[/eluser]
Thanks a lot for the example it works great also i wanted to share the code that I made from the MySQL code sample is very simple.

Code:
$i = 0;
if ($query->num_rows() > 0)
{
    foreach ($query->result() as $transaction)
    {
        // Taken from the MySQL sample this is only to get the balance.
        echo $i = ($i + $transaction->transaction_amount).'<br>';
    }
}

Thanks again




Theme © iAndrew 2016 - Forum software by © MyBB