I am creating bank ledger page but I need it to display the most recent balance at the top, currently it goes through the loop and adds and subtracts to get the balance at the bottom. Here is my code, let me know what the best solution to get this to work in reverse order with the least amount of code. Thanks!
$balance = 0.00;
// Get Results for Ledger, and Selected Time
$getResults = mysql_query("SELECT * FROM spd_ledger WHERE bankid='$actid' AND date <= '$to' AND date >= '$from' ORDER BY date ASC, plus DESC, minus DESC")or die(mysql_error());
while($row = mysql_fetch_array($getResults)) {
$id = $row['id'];
$type = $row['type'];
$plus = $row['plus'];
$minus = $row['minus'];
$memo = $row['memo'];
$date = $row['date'];
$accountid = $row['accountid'];
if($plus != '') {
$balance = $balance + $plus;
}
if($minus != '') {
$balance = $balance - $minus;
}
$getAccountName = mysql_query("SELECT * FROM spd_accounttypes WHERE id='$accountid'")or die(mysql_error());
while($row = mysql_fetch_array($getAccountName)) {
$account = $row['name'];
}
$plus = number_format($plus, 2, '.', '');
$minus = number_format($minus, 2, '.', '');
$balance = number_format($balance, 2, '.', '');
$date = date("m/d/Y", strtotime($date));
$results .= "<tr>
<td>$date</td>
<td>$type</td>
<td>$account</td>
<td>$memo</td>
<td>$plus</td>
<td>$minus</td>
<td>$balance</td>
<td><a href='deltransaction.php?id=$id' onClick='return confirm(\"Are you sure you want to delete this transaction?\");' class='btn btn-danger'><i class='icon-remove icon-white'></i></a></td>
</tr>";
}
$results = "<tr>
<td>$date</td>
<td>$type</td>
<td>$account</td>
<td>$memo</td>
<td>$plus</td>
<td>$minus</td>
<td>$balance</td>
<td><a href='deltransaction.php?id=$id' onClick='return confirm(\"Are you sure you want to delete this transaction?\");' class='btn btn-danger'><i class='icon-remove icon-white'></i></a></td>
</tr>" . $results;