0

所以我在MySQL数据库中有以下数据

id     date      type      source        particulars     amount    user
1      8/23/19   debit     General       load payment    500.00     1
2      8/24/19   credit    Loan          record loan     7,000.00   1
3      8/22/19   debit     General       grocery         800.00     1
4      8/25/19   credit    General       record salary   10,000.00  1
5      8/10/19   credit    General       other income    20,000.00  1

有了这些数据,我想实现以下目标:

  1. 按日、月和年对项目进行排序和分组
  2. 按用户过滤数据
  3. 创建余额表 - 逻辑是当类型为贷方时,执行加法,如果是借方,执行减法

我只能执行前两个任务,但似乎我无法连续执行数学运算。

我希望我的输出是这样的:

INCOME STATEMENT

Period covered: August 2019
Name: 1

8/25/19   credit    General       record salary   +10,000.00
8/22/19   debit     General       grocery         -   800.00 
8/23/19   debit     General       load payment    -   500.00
8/24/19   credit    Loan          record loan     + 7,000.00
8/25/19   credit    General       record salary   +10,000.00
                             CASH ON HAND          25,700.00

使用 if-else 条件,我只能做到这一点:

Period covered: August 2019
Name: 1

8/25/19   credit    General       record salary   +10,000.00
8/22/19   debit     General       grocery         -   800.00 
8/23/19   debit     General       load payment    -   500.00
8/24/19   credit    Loan          record loan     + 7,000.00
8/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>
                    <?php 
                        $query = $page->query("SELECT * FROM accounting");
                        $query->execute();

                        while ($row = $query->fetch(PDO::FETCH_OBJ))
                        {
                            ?> 
                            <tr>

                                <td><?php echo $row->id; ?></td>
                                <td><?php echo $row->type; ?></td>
                                <td><?php echo $row->source; ?></td>
                                <td><?php echo $row->particulars; ?></td>
                                <td><?php if ($row->type=='debit') { echo '-'; } else { echo '+'; } ?></td>
                                <td><?php echo $row->amount; ?></td>
                            </tr>
                            <?php   }
                   ?>
                   </tbody>
                   </table>
CASH ON HAND:

我无法呼应手头现金中金额列的总和/差异。

4

1 回答 1

2

您可以在处理行时保留一个总和,然后在最后输出:

$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>";
?>
于 2019-08-23T05:23:40.877 回答