gpt4 book ai didi

php - 支票簿余额 PHP

转载 作者:可可西里 更新时间:2023-11-01 08:08:06 25 4
gpt4 key购买 nike

我正在用 PHP 制作一个个人理财程序,但我却卡在了本应很容易完成的任务上 - 平衡账户。

交易存储在 MySQL 中,每个交易都有一个类型 - 1 用于取款,2 用于存款。以下是我用于平衡帐户的 PHP,没有接近正确的金额。

//Get the starting balance
$getStartingBalance = $db->query("SELECT amount FROM startingBalance WHERE id = 1");
$startingBalance = $getStartingBalance->fetch();
$startingBalance = $startingBalance['amount'];
//Balance transactions
$getAllTransactions = $db->query("SELECT * FROM transactions ORDER BY date");
while ($balanceEntry = $getAllTransactions->fetch()) {
$balanceEntryType = $balanceEntry['type'];
$balanceEntryAmount = $balanceEntry['amount'];
if ($balanceEntryType == "1") {
$accountBalance = $startingBalance - $balanceEntryAmount;
} else if ($balanceEntryType == "2") {
$accountBalance = $startingBalance + $balanceEntryAmount;
}
}

有什么想法吗?谢谢

最佳答案

$accountBalance = $startingBalance - $balanceEntryAmount;

这意味着每次点击此行时,您都会将当前余额设置为原始余额减去输入金额。你可能想在你的 while 循环之前分配:

# or just rename $startingBalance to $accountBalance
$accountBalance = $startingBalance;

然后在你的 while 循环中

$accountBalance = $accountBalance - $balanceEntryAmount;

当然,您还必须修复条件的其他分支。

下次您遇到此类错误时,请尝试在日志文件中输出每个计算的当前值 - 您会很快发现这一点。

关于php - 支票簿余额 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4456238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com