Reputation: 37
For example I have a variable $opening_balance = 5000; Now more transactions are taking place with debit and credit and I want to fetch data from database and want to print real time balance at the end of each row. Following is is an example what I want exactly in my results.
----------------------------------------------------
| Sr No. | Description | Debit | Credit | Balance |
----------------------------------------------------
| 1. | Debit | 500 | - | 4500 |
| 1. | Credit | - | 1000 | 5500 |
| 1. | Debit | 200 | - | 5300 |
----------------------------------------------------
Following is my code: -
$query = mysqli_query($conn, "SELECT * FROM patty_cash WHERE month(date) = '11' AND year(date) = '2020' ");
while($row = mysqli_fetch_assoc($query))
{
extract($row);
$newBalance = $opening_balance + $credit - $debit;
echo "<tr>
<td>$sr_no</td>
<td>$description</td>
<td>$debit</td>
<td>$credit</td>
<td>$newBalance</td>
</tr>";
}
But I am not getting accurate balance. Please suggest what to do to get accurate balance.
Upvotes: 0
Views: 849
Reputation: 132
Add at the top $balance
to initialize the data first and use it inside while
rather than $opening
balance.
$balance = $opening_balance;
$query = mysqli_query($conn, "SELECT * FROM patty_cash WHERE month(date) = '11' AND year(date) = '2020' ");
while($row = mysqli_fetch_assoc($query))
{
extract($row);
$balance = $balance + $credit - $debit;
echo "<tr>
<td>$sr_no</td>
<td>$description</td>
<td>$debit</td>
<td>$credit</td>
<td>$balance</td>
</tr>";
}
Upvotes: 1