首页 > 解决方案 > 如何为表格的一列设置倒计时?

问题描述

我想为表格的一列设置一个计时器,该列包含时间,我想为此列设置一个计时器。此列的每个单元格都有一个特定的时间,如果该时间是该小时的 20 分钟前的 1 小时,它应该显示一个弹出消息,说明时间即将结束。

下面是我的表:

栏目名称:时差

<body>
    <div class="card-body">
        <table class="table">
            <thead>
            <tr>
                <th>IdCard</th>
                <th>Name</th>
                <th>Company</th>
                <th>Floors</th>
                <th>Arrival</th>
                <th>Departure</th>
                <th>Time Difference</th>
            </tr>
            </thead>
<?php
include('includes/dbconnection.php');
$sql="SELECT *,
            TIME_FORMAT(arrival, '%h:%i %p') AS arrival,
            TIME_FORMAT(departure, '%h:%i %p') AS departure,
            TIME_FORMAT(timediff(departure, arrival), '%H:%i') AS difftime
        FROM master where Status = 'Pending'";

$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);

$cnt=1;
if($query->rowCount() > 0) {
    foreach($results as $row) {
?>   

            <tr>
                <!-- <td><?php echo htmlentities($cnt);?></td>-->
                <td><?php  echo htmlentities($row->idcard);?></td>
                <td><?php  echo htmlentities($row->name);?></td>
                <td><?php  echo htmlentities($row->company);?></td>
                <td><?php  echo htmlentities($row->floors);?></td>
                <td><?php  echo htmlentities($row->arrival);?></td>
                <td><?php  echo htmlentities($row->departure);?></td>
                <td><span style="color: red;"><?php  echo htmlentities($row->difftime);?></span></td>
            </tr>
<?php 
        $cnt=$cnt+1;
    }
} 
?>   

        </table>
    </div>                                  
</body>

表格图像

标签: phphtml

解决方案


埃利亚娜,

请参阅这些答案如何通过浏览器刷新 PHP 页面:

  1. 使用 PHP 刷新页面
  2. PHP脚本自行刷新并重新启动执行时间

页面刷新后,您当前的代码将在最后一列“时差”中显示新的时差。

如果您仍然想显示弹出窗口,那么您需要使用一些 Javascript 在页面加载后显示弹出窗口,例如:

<body onload="showArrivingSoon()">

<!-- Your code ... -->

<script>
    <?php
        // Iterate through rows to produce string $arrivingSoonAsStringDelimitedByNewLine ...
    ?>

    // Here the prepared string is printed to the page and initialize JS variable
    var arrivingSoon = '<?php echo $arrivingSoonAsStringDelimitedByNewLine ?>';

    function showArrivingSoon(arrivingSoon)
    {
        alert('Arriving soon:\n' + arrivingSoon);
    }
</script>
</body>

迭代、计算和检查也可以在 Javascript 中完成。要做到这一点,首先将数据行作为 JSON 从 PHP 传递到 Javascript 变量,然后进行迭代会更容易。


推荐阅读