Jquery的或JavaScript在HTML表中添加计算柱[复制]

问题描述 投票:0回答:2

我生成PHP与MySQL查询的HTML表(表看起来像这样https://ibb.co/2k8XbbZ

DB ID   EmpID   Date        Username    Computername    State   Minutesatstate  Statestarttime  Stateendtime        Timestamp
704634  303836  02-06-2019  user        PC-818          Idle    2               13:44           13:46               2019-02-06 13:46:46
704599  303836  02-06-2019  user        PC-818          Active  16              13:28           13:44               2019-02-06 13:44:46
704340  303836  02-06-2019  user        NIPL-1220       Active  2               13:27           13:28               2019-02-06 13:28:48
704313  303836  02-06-2019  user        PC-818          Active  13              13:15           13:27               2019-02-06 13:27:31

我想补充的jQuery或Javascript角的另一列减去第1行与第2行Stateendtime列Statestarttime列找出时间差

例如预期的结果

StateStarTtime    StateEndTime   Min Difference 

03:57             03:58          00:03
03:53             03:54          00:04
03:46             03:49          null

我能找到这么多例子,帮助第一行从StateEndTime减去StateStarTtime,但我想减去StateEndTime的第二行单元格值StateStarTtime的第1行单元格值找到分差。

我在HTML和PHP初学者,但完全新的JS / JQ等我怎么能做到这一点的任何方向是非常赞赏。下面是PHP和HTML代码片断我使用。让我知道如果你想要完整的代码。

            <?php while($row = mysqli_fetch_array($search_result)):?>

            <tr>
                <td align="Center"><?php echo $row['id'];?></td>
                <td align="Center"><?php echo $row['EmpID'];?></td>
                <td align="Center"><?php echo $row['Date'];?></td>
                <td align="Center"><?php echo $row['Username'];?></td>
                <td align="Center"><?php echo $row['Computername'];?></td>
                <td align="Center"><?php echo $row['State'];?></td>
                <td align="Center"><?php echo $row['MinutesatState'];?></td>
                <td align="Center"><?php echo $row['StateStarttime'];?></td>
                <td align="Center"><?php echo $row['StateEndtime'];?></td>
                <td align="Center"><?php echo $row['Timestamp'];?></td>


            </tr>
            <?php endwhile;?>
javascript php jquery html
2个回答
1
投票

请尝试使用另一个循环,然后计算其如下存储StateStarTtime和StateEndTime阵列,因为你不能让单圈下一行的值

<?php while($row = mysqli_fetch_array($search_result)):?>
    <?php $startTime[] =  $row['StateStarttime'];?>
    <?php $endTime[] = $row['StateEndtime'];?>
<?php endwhile;?>
<?php 
    $i = 0;
    while($row = mysqli_fetch_array($search_result)):?>
        <tr>
            <td align="Center"><?php echo $row['id'];?></td>
            <td align="Center"><?php echo $row['EmpID'];?></td>
            <td align="Center"><?php echo $row['Date'];?></td>
            <td align="Center"><?php echo $row['Username'];?></td>
            <td align="Center"><?php echo $row['Computername'];?></td>
            <td align="Center"><?php echo $row['State'];?></td>
            <td align="Center"><?php echo $row['MinutesatState'];?></td>
            <td align="Center"><?php echo $row['StateStarttime'];?></td>
            <td align="Center"><?php echo $row['StateEndtime'];?></td>
            <td align="Center"><?php echo $row['Timestamp'];?></td>
            //your calculation goes here like diff



            <td align="Center">
               <?php 
                   $mycalc = ($startTime[$i] - endTime[$i+1]);   
                    echo $mycalc;
                ?>
            </td>
        </tr>
        $i++;
<?php endwhile;?>

0
投票

你可以简单地用它做在PHP

 <td align="Center"><?php echo($row['StateEndtime'] - $row['StateStarTtime ']) ?></td>
© www.soinside.com 2019 - 2024. All rights reserved.