Jump to content

Last transactions and displaying last balance after it


edgarasm
Go to solution Solved by kicken,

Recommended Posts

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>

dued.png

 

Link to comment
Share on other sites

  • Solution

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.
}
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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:(

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.