【问题标题】:How to set countdown timer for a column of a table?如何为表格的一列设置倒计时?
【发布时间】:2020-09-28 16:19:12
【问题描述】:

我想为表格的一列设置一个计时器,该列包含时间,我想为此列设置一个计时器。该列的每个单元格都有一个特定的时间,如果该时间是该小时的 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>

Table Image

【问题讨论】:

  • 为什么有 2 个 &lt;tr&gt; 标签相互跟随
  • 良好的代码缩进将帮助我们阅读代码,更重要的是它将帮助您调试您的代码Take a quick look at a coding standard为您自己的利益。您可能会被要求在几周/几个月内修改此代码,最后您会感谢我的。
  • @RiggsFolly 已编辑

标签: php html


【解决方案1】:

埃莉安娜,

查看这些答案如何通过浏览器刷新 PHP 页面:

  1. Refresh a page using PHP
  2. PHP-script refresh it self and restart execution-time

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

如果你还想显示弹窗,那么你需要使用一些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 变量,然后进行迭代会更容易。

【讨论】:

    猜你喜欢
    • 2023-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-18
    • 2016-06-17
    相关资源
    最近更新 更多