php - 如何为表格的一列设置倒计时?
问题描述
我想为表格的一列设置一个计时器,该列包含时间,我想为此列设置一个计时器。此列的每个单元格都有一个特定的时间,如果该时间是该小时的 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>
解决方案
埃利亚娜,
请参阅这些答案如何通过浏览器刷新 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 变量,然后进行迭代会更容易。
推荐阅读
- scala - 如何在数据框scala中的地图列类型上执行agg函数
- security - 安全上下文获取变量
- bash - 如何同时从多个目录中执行 SLURM 脚本?
- r - 如何重命名 R 中 SpatialPolygonsDataFrame 的特定列中的条目?
- javascript - 如何随机分配到治疗组进行在线网络学习?
- javascript - 如何在另一页中获取 json 数据?
- javascript - 在 StreamDeckSDK 中正确解析新行上的 Json 数组
- r - 与同事分享 R 闪亮应用程序的各种方式
- sql - 案例语法 - 减去 2 个值会给出不正确的数字
- python - join() 参数必须是 str 或字节,而不是“元组”——在 Django 中制作报纸项目时