所以我在 MySQL 数据库中有以下数据id date type source particulars amount user1 8/23/19 debit General load payment 500.00 12 8/24/19 credit Loan record loan 7,000.00 13 8/22/19 debit General grocery 800.00 14 8/25/19 credit General record salary 10,000.00 15 8/10/19 credit General other income 20,000.00 1有了这些数据,我想实现以下目标:按日、月和年对项目进行排序和分组按用户过滤数据创建余额表 - 逻辑是当类型为贷方时,则执行加法,如果为借方,则执行减法我只能执行我拥有的前两个任务,但似乎我无法连续执行数学运算。我希望我的输出是这样的:INCOME STATEMENTPeriod covered: August 2019Name: 18/25/19 credit General record salary +10,000.008/22/19 debit General grocery - 800.00 8/23/19 debit General load payment - 500.008/24/19 credit Loan record loan + 7,000.008/25/19 credit General record salary +10,000.00 CASH ON HAND 25,700.00使用 if-else 条件,我只能做到这一点:Period covered: August 2019Name: 18/25/19 credit General record salary +10,000.008/22/19 debit General grocery - 800.00 8/23/19 debit General load payment - 500.008/24/19 credit Loan record loan + 7,000.008/25/19 credit General record salary +10,000.00到目前为止我尝试过的代码。 <table id="myTable" class="table table-bordered table-striped"> <thead> <tr> <th>ID</th> <th>TYPE</th> <th>SOURCE</th> <th>PARTICULARS</th> <th></th> <th>AMOUNT</th> </tr> </thead> <tbody>我无法回应手头现金中金额列的总和/差异。
1 回答
蝴蝶刀刀
TA贡献1801条经验 获得超8个赞
您可以在处理行时保持总和,然后在最后输出:
$cash_on_hand = 0;
while ($row = $query->fetch(PDO::FETCH_OBJ))
{
$cash_on_hand += ($row->type == 'debit' ? -1 : 1) * $row->amount;
>?
// output each row
<?php
}
echo "<tr><td colspan=\"5\">CASH ON HAND</td><td>$cash_on_hand</td></tr>";
?>
- 1 回答
- 0 关注
- 168 浏览
添加回答
举报
0/150
提交
取消