在一个财务跟踪器上工作时,遇到了一个小问题,即用正确的运行余额按降序(从最新到最旧)显示事务。
下面是我正在使用的SQL:SELECT amount, payee, cat, date FROM transactions ORDER BY id DESC, date DESC
我正在使用一个类来处理这个应用程序的其他方面,显示事务的函数是它的一部分。如果我按升序显示事务,则以下代码将按预期工作:
//Fetch the transactions
public function fetchTransactions() {
global $db;
$this->getStartingBalance();
$runningTotal = $this->startingBalance;
$fetchTransactionsSQL = $db->query("SELECT amount, payee, cat, date FROM transactions ORDER BY id, date");
while ($transactionDetails = $fetchTransactionsSQL->fetch()) {
$payee = stripslashes($transactionDetails[payee]);
$category = $transactionDetails['cat'];
$amount = money_format("%n", $transactionDetails[amount]);
$runningTotal = $runningTotal + $transactionDetails['amount'];
$runningTotalOutput = money_format("%n", $runningTotal);
echo "
<tr>
<td class=\"payee\">$payee</td>
<td>$category</td>
<td>$amount</td>
<td class=\"runningTotal\">$runningTotalOutput</td>
</tr>";
}
}
坚持尝试按降序显示正确的运行余额-例如,如果
$this->startingBalance
的值为$354.00,并且第一个(因此最后一个按降序显示的)事务为$4.00,则最后一个条目的运行余额将为$350.00。号码都签了名。非常感谢!
更新的代码
按照drew010的建议,我想起了一个方便的小函数数组。我更新了函数以包含一个事务数组,并对其进行了反转,然后遍历该数组以使用正确的运行余额按升序显示事务。
我刚刚遇到的另一个问题是索引事务,例如,只显示当前月份的事务,同时保持正确的运行平衡。下面更新的代码解决了以正确的余额顺序显示所有事务的问题,但无助于限制返回的行。
这是最新的代码,建议不胜感激。
public function fetchTransactions() {
global $db;
$this->getStartingBalance();
$runningTotal = $this->startingBalance;
//Array to contain the transactions
$transactions = array();
//Counter for transactions array key
$i = 0;
$fetchTransactionsSQL = $db->query("SELECT amount, payee, cat, date FROM transactions ORDER BY id, date");
while ($transactionDetails = $fetchTransactionsSQL->fetch()) {
$payee = stripslashes($transactionDetails[payee]);
$category = $transactionDetails['cat'];
$amount = money_format("%n", $transactionDetails[amount]);
$runningTotal = $runningTotal + $transactionDetails['amount'];
$runningTotalOutput = money_format("%n", $runningTotal);
$transactions[$i] = array("payee" => $payee, "category" => $category, "amount" => $amount, "runningTotal" => $runningTotalOutput);
$i++; //Increment the array key
}
$transactions = array_reverse($transactions); //Reverse the transactions array to display in ascending order
//Iterate over transactions array
foreach ($transactions as $transaction) {
$payee = $transaction['payee'];
$category = $transaction['category'];
$amount = $transaction['amount'];
$runningTotal = $transaction['runningTotal'];
echo "
<tr>\n
<td>$payee</td>\n
<td>$category</td>\n
<td>$amount</td>\n
<td>$runningTotal</td>\n
</tr>\n
";
}
}
最佳答案
来自交易记录的运行总和(金额)
记住结果并将其添加到$startingBalance中,并将其记为$endingBalance
运行查询以按所需的描述顺序选择所有事务
对于每笔交易,不要在$running balance中添加$amount,而是从$endingBalance中减去$amount。。。
例子:
按说明顺序选择的5个交易记录(仅金额):
一。-7个
2。十
三。二十二
四。-17岁
5个。-2个
交易总额:$tSum=-7+10+22-17-2=6
$startingBalance=300美元
$endingBalance=$startingBalance+tSum=300+6=306
按描述顺序显示的事务:
(金额|流动压载)
期末余额:306
-7 | 306($期末余额)
10 | 313($endingBalanse-=-7)
22 | 303($endingBalance-=10)
-17 | 281($endingBalance-=22)
-2 | 298($endingBalance-=-17)
起始余额:300($endingBalance-=-2)
您的代码已更新:
//Fetch the transactions public
function fetchTransactions()
{
global $db;
$this->getStartingBalance();
$endingBalance = $this->startingBalance + $this->getTransactionsSum();
$runningTotal = $endingBalance;
$prevAmount = 0;
$fetchTransactionsSQL = $db->query("SELECT amount, payee, cat, date FROM transactions ORDER BY id DESC, date DESC");
while ($transactionDetails = $fetchTransactionsSQL->fetch())
{
$payee = stripslashes($transactionDetails[payee]);
$category = $transactionDetails['cat'];
$amount = money_format("%n", $transactionDetails[amount]);
$runningTotal -= $prevAmount;
$prevAmount = $amount;
$runningTotalOutput = money_format("%n", $runningTotal);
echo "<tr>
<td class=\"payee\">$payee</td>
<td>$category</td>
<td>$amount</td>
<td class=\"runningTotal\">$runningTotalOutput</td>
</tr>";
}
}
function getTransactionsSum()
{
$sql = $db->query("SELECT SUM(amount) AS mySum FROM transactions");
if( $sumFields = $sql->fetch() )
{
return money_format("%n", $sumFields[mySum]);
}
else
{
return 0;
}
}
我希望你已经找到了帮助。。。