You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP中while循环计算数组值差值的表格显示位置问题

解决PHP循环中表格差值显示位置错误的问题

看起来你已经把差值计算的逻辑调通了,只是显示位置有问题,而且第一行的差值因为$i-1为-1,意外取到了数组最后一个元素,导致数值异常。咱们一步步来修正:

问题分析

当前代码里,每一行的Difference列计算的是上一行总量 - 当前行总量,并显示在当前行。你提到“差值本应显示在表格的下一行,却出现在当前行”,结合你的需求推测:你希望当前行和下一行的差值,显示在下一行的Difference列中,同时第一行因为没有上一行,差值应该留空或显示标记。

另外当前代码在$i=0时,$s[$i-1]会取数组最后一个元素,导致第一行差值完全错误,这也是需要优先修正的点。

解决方案1:修正第一行错误,保持“下一行显示当前行与下一行的差值”逻辑

先提前计算好所有行的差值,再循环输出表格,这样能精准控制每个位置的差值内容:

<table id="meter_entries" class="table table-striped table-hover dt-responsive">
    <thead>
        <tr>
            <th>ID</th>
            <th>Date</th>
            <th>Company Code</th>
            <th>Oil Height</th>
            <th>Water Height</th>
            <th>Total Volume</th>
            <th>Difference</th>
            <th>Actions</th>
        </tr>
    </thead>
    <?php 
    $s = $stock_automatic;
    $count = count($s);
    // 先提前计算所有行的差值
    $diffs = [];
    for($j = 0; $j < $count; $j++){
        if($j === $count - 1){
            // 最后一行没有下一行,差值留空或显示标记
            $diffs[] = '-';
        } else {
            // 计算当前行与下一行的差值,按你的需求调整正负
            $currvol = $s[$j]['total_volume'];
            $nextvol = $s[$j+1]['total_volume'];
            $diff = $currvol - $nextvol;
            $diffs[] = $diff;
        }
    }
    // 循环输出表格行
    $i = 0;
    while($i < $count){ 
    ?>
    <tr>
        <td><?php echo $s[$i]['id']; ?></td>
        <td><?php echo $s[$i]['timess']; ?></td>
        <td><?php echo $s[$i]['company_code']; ?></td>
        <td><?php echo $s[$i]['oil_height']; ?></td>
        <td><?php echo $s[$i]['water_height']; ?></td>
        <td><?php echo $s[$i]['total_volume']; ?></td>
        <td><?php echo $diffs[$i]; ?></td>
        <td>
            <a href="<?php echo site_url('stock_automatic/edit/'.$s[$i]['id']); ?>" class="btn btn-info btn-xs"><span class="fa fa-pencil"></span> Edit</a>
            <a href="<?php echo site_url('stock_automatic/remove/'.$s[$i]['id']); ?>" class="btn btn-danger btn-xs"><span class="fa fa-trash"></span> Delete</a>
        </td>
    </tr>
    <?php 
    $i++; 
    } 
    ?>
</table>

解决方案2:如果需求是“当前行显示与上一行的差值”

如果你实际需求是从第二行开始,显示当前行与上一行的差值(比如第二行显示第一行减第二行的结果),那只需要给第一行的差值留空,避免越界错误即可:

<table id="meter_entries" class="table table-striped table-hover dt-responsive">
    <thead>
        <tr>
            <th>ID</th>
            <th>Date</th>
            <th>Company Code</th>
            <th>Oil Height</th>
            <th>Water Height</th>
            <th>Total Volume</th>
            <th>Difference</th>
            <th>Actions</th>
        </tr>
    </thead>
    <?php 
    $s = $stock_automatic;
    $count = count($s);
    $i = 0;
    while($i < $count){ 
    ?>
    <tr>
        <td><?php echo $s[$i]['id']; ?></td>
        <td><?php echo $s[$i]['timess']; ?></td>
        <td><?php echo $s[$i]['company_code']; ?></td>
        <td><?php echo $s[$i]['oil_height']; ?></td>
        <td><?php echo $s[$i]['water_height']; ?></td>
        <td><?php echo $s[$i]['total_volume']; ?></td>
        <td>
            <?php 
            // 第一行没有上一行,差值留空
            if($i === 0){
                echo '-';
            } else {
                $oldvol = $s[$i-1]['total_volume'];
                $currvol = $s[$i]['total_volume'];
                $diff = $oldvol - $currvol;
                echo $diff;
            }
            ?>
        </td>
        <td>
            <a href="<?php echo site_url('stock_automatic/edit/'.$s[$i]['id']); ?>" class="btn btn-info btn-xs"><span class="fa fa-pencil"></span> Edit</a>
            <a href="<?php echo site_url('stock_automatic/remove/'.$s[$i]['id']); ?>" class="btn btn-danger btn-xs"><span class="fa fa-trash"></span> Delete</a>
        </td>
    </tr>
    <?php 
    $i++; 
    } 
    ?>
</table>

额外提示

如果你的数组是按时间倒序排列的(最新记录在最前面),可以调整差值的计算顺序(比如$diff = $nextvol - $currvol),让差值的正负符合你的业务逻辑。另外注意处理数组为空的情况,避免循环报错。

内容的提问来源于stack exchange,提问作者Enoch

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:45:31