PHP-仅在IF语句更改时显示计算

时间:2018-10-28 10:01:55

标签: php if-statement

这是我的代码:

        <table class="table table-bordered">
        <thead>
            <tr>
                <th>Sr#</th>
                <th>Description</th>
                <th>Sale</th>
                <th>Expense</th>
            </tr>
        </thead>    
        <tbody>
            <?php
            $fetch_record = "SELECT * FROM `transactions` JOIN `users` ON `transactions`.`user_id`=`users`.`user_id` WHERE `users`.`user_id`='$user_id'";
            $fetch_record_r = mysqli_query($con, $fetch_record);
            $record_found = mysqli_num_rows($fetch_record_r);
            if ($record_found > 0) {
                $sr = 1;
                $net_total = 0;
                while($records = mysqli_fetch_assoc($fetch_record_r)){
                    if ($records['type'] == 1) {
                        $net_total = $net_total + $records['sale'];
                    }else if($records['type'] == 2){
                        $net_total = $net_total - $records['expense'];
                    }
            ?>
                    <tr>
                        <td><?php echo $sr++; ?></td>
                        <td style="text-transform: capitalize;"><?php echo $records['description']; ?></td>
                        <td><?php echo $records['sale'];?></td>
                        <td><?php echo $records['expense'];?></td>
                    </tr>
                    <?php
                    if (($sr % 2)) { // skip even members
                        ?>
                        <tr>
                            <td colspan="2"></td>
                            <td colspan="2" class="label-info text-center" style="color: #fff;">Total Balance: <?php echo $net_total; ?></td>
                        </tr>
                        <?php
                    }
                    ?>
            <?php
                }
                    ?>
                    <tr>
                        <td colspan="4" class="label-info text-center" style="color: #fff;">Total: <?php echo $net_total; ?></td>
                    </tr>
                    <?php
            }else{
                echo "<h1 class='text-center text-danger'>No Record Found</h1>";
            }
            ?>
        </tbody>
    </table>

数据来自数据库,而不是计算。我希望当交易类型从销售变为费用时,应计算所有与销售相关的交易,反之亦然。就像资产负债表一样。我知道我写错了逻辑。但我无法解决逻辑。通过查看图片,您可以更清楚地了解我的问题。Sale expense calculator example

2 个答案:

答案 0 :(得分:0)

您可以使用一个临时变量来指示type值是否已从一个迭代更改为另一个迭代。

如果这是我的代码,为清楚起见,我将尝试在SELECT子句中命名列。 我还认为在查询中添加ORDER BY子句是明智的,但我不知道您要对哪种列名进行排序。

未经测试的代码:

<?php
$sql = "SELECT * FROM transactions a INNER JOIN `users` b ON a.user_id = b.user_id WHERE a.user_id = " . (int)$user_id;  // ORDER BY date (or something?)
if (!$result = mysqli_query($con, $sql)) {
    echo '<tr><td colspan="4"><h1 class="text-center text-danger">Query Syntax Error</h1></td></tr>';
} elseif (!mysqli_num_rows($fetch_record_r)) {
    echo '<tr><td colspan="4"><h1 class="text-center text-danger">No Record Found</h1></td></tr>';
} else {
    $sr = 0;
    $net_total = 0;
    $type = null;
    while ($row = mysqli_fetch_assoc($result)) {
        if ($type !== null && $type != $row['type']) {
            // type has change from previous and is not null
            ?>
            <tr>
                <td colspan="2"></td>
                <td colspan="2" class="label-info text-center" style="color: #fff;">Total Balance: <?php echo $net_total; ?></td>
            </tr>
            <?php
        }

        if ($row['type'] == 1) {
            $net_total += $row['sale'];
        } elseif ($row['type'] == 2) {
            $net_total -= $row['expense'];
        }

        ?>
        <tr>
            <td><?php echo ++$sr; ?></td>
            <td style="text-transform: capitalize;"><?php echo $row['description']; ?></td>
            <td><?php echo $row['sale'];?></td>
            <td><?php echo $row['expense'];?></td>
        </tr>
        <?php
        $type = $row['type'];  // update the temporary variable
    }
    ?>
    <tr>
        <td colspan="4" class="label-info text-center" style="color: #fff;">Total: <?php echo $net_total; ?></td>
    </tr>
    <?php
}

答案 1 :(得分:0)

当收入中的条目数多于一项时,您似乎正在尝试获取值的总和?

如果是这样,请考虑采用以下方法以获取全部信息。

SELECT SUM(**column_name**)
FROM **table_name**
WHERE **condition**; 

您可以将结果分配给变量,并使用if语句填充结果。

您可以使用if语句检查返回的行数是否大于一,执行以上SQL语句,并填充计算/结果。