在包含金额的 while 循环中的问题

Issue inside of while loop with amounts

提问人:user3236149 提问时间:3/6/2023 最后编辑:Dharmanuser3236149 更新时间:3/7/2023 访问量:33

问:

我在我的时间里遇到了一个问题,数据是按日期和 id 用户分组的,但在每天的总金额中,金额与第二天的第一个金额相加,因此每天的总金额不正确。

我该如何解决这个问题?

这是我的查询:

<table class="table table-bordered table-striped table-condensed">
    <thead>
        <tr>
            <th>Date</th>
            <th>Username</th>
            <th>Total per day</th>
            <th>Income</th>
            <th>expenses</th>
            <th>Total</th>
        </tr>
    </thead>
    <tbody>
        <?php
            $day = 01;
            $sql = $conn->prepare("SELECT count(id) AS id, DATE_FORMAT(payDay, '%d %M %Y') AS date, DATE_FORMAT(payDay, '%d') AS day,
            SUM(total) AS total, username, SUM(amount) AS amount
            FROM INCOME WHERE active != 0 AND MONTH(payDay) = MONTH(current_date) AND YEAR(payDay) = YEAR(current_date)
            GROUP BY date ASC, id_user ASC");
            $sql->execute();
            while($row = $sql->fetch(PDO::FETCH_ASSOC)) { 
                //sum totally at final
                $totalPerMonth += $row['total'];
                $AmountPerMonth+= $row['amount'];
                $usersPerMonth += $row['id'];
                //totally per day
                $totalPerDay += $row['total'];
                $amountPerDay += $row['amount'];
                $UserPerDay += $row['id'];
                
                $ddate = $row['day'];
                
        ?>
        <?php if($day != $ddate){ ?>
        <tr>
            <td colspan="2">Total per day</td>
            <td class="center"><?= $UserPerDay; ?></td>
            <td class="center"><?= number_format($totalPerDay,2,'.',','); ?></td>
            <td class="center"><?= number_format($amountPerDay,2,'.',','); ?></td>
            <td class="center"><?= number_format(($totalPerDay+$amountPerDay),2,'.',','); ?></td>
        </tr>
        <tr>
            <td colspan="6"> --------- </td>
        </tr>
        <?php  $UserPerDay = NULL; $totalPerDay = NULL; $amountPerDay = NULL; } ?>
        <tr>
            <td><?= $row['date']; ?></span></td>
            <td class="center"><?= $row['username']; ?></td>
            <td class="center"><?= $row['id']; ?></td>
            <td class="center"><?= number_format($row['total'],2,'.',','); ?></td>
            <td class="center"><?= number_format($row['amount'],2,'.',','); ?></td>
            <td class="center"><?= number_format($row['total']+$row['amount'],2,'.',','); ?></td>
        </tr>
        <?php  $dd = $ddate; $row['total'] = NULL; $row['amount'] = NULL; } ?>
    </tbody>
    <tbody>
        <tr>
            <td class="text-center" colspan="3"><?= $usersPerMonth; ?></span></td>
            <td class="text-center" colspan="3"><?= number_format($totalPerMonth+$AmountPerMonth,2,'.',','); ?></td>
        </tr>
    </tbody>
</table>

因此,每个示例,如果 X 个月的第一天总共有 50,第二天的总数是 15,那么第一天的总和是 50 + 15......

这里捕获:

image capture

php mysql while-loop pdo

评论

0赞 Barmar 3/7/2023
@derpirscher 为什么会这样呢?不同之处在于,每当日期更改时,变量都会重置。PerDay
0赞 derpirscher 3/7/2023
@Barmar啊,你是对的,错过了......
0赞 CBroe 3/7/2023
你已经这样做了,甚至在你检查这一天是否还是一样的之前,$totalPerDay += $row['total'];<?php if($day != $ddate){ ?>

答: 暂无答案