edgarasm Posted November 21, 2013 Share Posted November 21, 2013 Hello I got a question , Im trying to add balance after each transaction ,what I tried to do is to echo $account Balance and then use function to deduct the row amount Im really confused and can't figure it out how i would i achieve such thing : The code I've used <td>£ <?php echo number_format ($account['balance'] + $row['amount'], 2); ?></td> Quote Link to comment https://forums.phpfreaks.com/topic/284138-last-transactions-and-displaying-last-balance-after-it/ Share on other sites More sharing options...
dalecosp Posted November 21, 2013 Share Posted November 21, 2013 Wow, you've got 7M pounds? Could I have 100,000 or so? What's wrong with the way you're doing it? It appears to be working ... Quote Link to comment https://forums.phpfreaks.com/topic/284138-last-transactions-and-displaying-last-balance-after-it/#findComment-1459385 Share on other sites More sharing options...
Solution kicken Posted November 21, 2013 Solution Share Posted November 21, 2013 You need to do a running balance and keep track of the changes as you go. So you'd start with the initial balance and for each row, add the amount to the running balance and save the new running balance. <?php $runningBalance = 0; foreach ($transactions as $tran){ $runningBalance += $tran['amount']; //Echo all your stuff, echo $runningBalance in the balance column. } Quote Link to comment https://forums.phpfreaks.com/topic/284138-last-transactions-and-displaying-last-balance-after-it/#findComment-1459389 Share on other sites More sharing options...
edgarasm Posted November 21, 2013 Author Share Posted November 21, 2013 You need to do a running balance and keep track of the changes as you go. So you'd start with the initial balance and for each row, add the amount to the running balance and save the new running balance. <?php $runningBalance = 0; foreach ($transactions as $tran){ $runningBalance += $tran['amount']; //Echo all your stuff, echo $runningBalance in the balance column. } I have added the code you gave still no luck now the page errors :/ PHP Warning: Invalid argument supplied for foreach() in C:\Inetpub\vhosts\tugapay.com\httpdocs\account\dashboard.php on line 93 Quote Link to comment https://forums.phpfreaks.com/topic/284138-last-transactions-and-displaying-last-balance-after-it/#findComment-1459393 Share on other sites More sharing options...
edgarasm Posted November 21, 2013 Author Share Posted November 21, 2013 Wow, you've got 7M pounds? Could I have 100,000 or so? :PWhat's wrong with the way you're doing it? It appears to be working ... The problem is that it always makes the balances different , if I have the top balance different the bottom ones will change as I'm grabbing the top value really frustrates me ugh:( Quote Link to comment https://forums.phpfreaks.com/topic/284138-last-transactions-and-displaying-last-balance-after-it/#findComment-1459394 Share on other sites More sharing options...
dalecosp Posted November 21, 2013 Share Posted November 21, 2013 I have added the code you gave still no luck now the page errors :/ PHP Warning: Invalid argument supplied for foreach() in C:\Inetpub\vhosts\tugapay.com\httpdocs\account\dashboard.php on line 93 You don't paste the code directly; use your own variables ;) Quote Link to comment https://forums.phpfreaks.com/topic/284138-last-transactions-and-displaying-last-balance-after-it/#findComment-1459406 Share on other sites More sharing options...
kicken Posted November 21, 2013 Share Posted November 21, 2013 I have added the code you gave still no luck now the page errors :/ My code was not a copy-and-paste fix, it's just an example of the process. You need to figure out how to integrate that procedure into your existing code. Quote Link to comment https://forums.phpfreaks.com/topic/284138-last-transactions-and-displaying-last-balance-after-it/#findComment-1459407 Share on other sites More sharing options...
edgarasm Posted November 22, 2013 Author Share Posted November 22, 2013 Been trying since yesterday and can't figure it out :/ Mostly i get the error i provided before Quote Link to comment https://forums.phpfreaks.com/topic/284138-last-transactions-and-displaying-last-balance-after-it/#findComment-1459522 Share on other sites More sharing options...
mac_gyver Posted November 22, 2013 Share Posted November 22, 2013 since you have provided zero actual information in this thread about what your code and data is, except for maybe your $row['amount'] variable, how could anything posted in this thread be anything more than an example/outline of a way of doing what you asked? programming isn't about randomly copy/pasting together things that you have seen or found. its about writing code, line by line, that does specifically what you want. in order to do that you must first define the steps that accomplish what you want and you must actually know what each statement does so that when you put statements together they contribute toward the goal you are trying to accomplish. if you currently have a loop that is iterating over the data from your query, couldn't you just take the example LOGIC that kicken posted and at the point where he showed a foreach() loop, use the existing loop in your code? Quote Link to comment https://forums.phpfreaks.com/topic/284138-last-transactions-and-displaying-last-balance-after-it/#findComment-1459525 Share on other sites More sharing options...
edgarasm Posted November 22, 2013 Author Share Posted November 22, 2013 thanks guys helped me a lot all sorted now Quote Link to comment https://forums.phpfreaks.com/topic/284138-last-transactions-and-displaying-last-balance-after-it/#findComment-1459541 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.