仅在插入或更新新数据时自动刷新/加载数据


Auto refresh/load data only if new data inserted or updated

在我的情况下,我在X秒后进行自动刷新。但我想让它只在插入或更新新数据时刷新。

这是我的剧本。

刷新.php

<script src="jquery-1.4.js"></script>
<script>
$(document).ready(function() {
$("#responsecontainer").load("refreshnow.php");
var refreshId = setInterval(function() {
$("#responsecontainer").load('refreshnow.php?randval='+ Math.random());
}, 1000);
});
</script>
<div id="responsecontainer"></div>

refreshnow.php

<?php
$query = mysqli_query($connection,"select * from `table`");
$rows = mysqli_num_rows($query);
?>
<table style="background:#F0F0F0;" border=1 style="border-collapse:collapse">
 <tr>
    <th>Name</th>
    <th>Job</th>
 </tr>
<tbody>
<?php
while($result = mysqli_fetch_array($query)){
    echo "<tr>";
    echo "<td>".$result["name"]."</td>";
    echo "<td>".$result["job"]."</td>";
    echo "</tr>";
}
?>
</tbody>

那么,当x秒后插入或更新新数据而不刷新时,是否可以自动加载新数据?

如果你想使用现有的代码,只需在mysql表中添加值为time()的另一列(如果你有一个配置或设置表,这会更容易,如果不是为你插入的每个记录添加广告时间戳的话),这会在每次插入/修改行时更新数据库中的时间戳。然后只需执行以下查询:

"select timestamp_column from `settings_table` order by timestamp_column limimt 1"
If ($result["timestamp_column"]+1000>=time()){
       //do the refreshnow.php code here
}

希望能有所帮助,干杯