我有这个PHP页面连接到SQLite数据库并从中读取数据。数据不断更新,我需要网页刷新每分钟显示的数据。怎么做?我看到Ajax有助于此,但我是一个完整的Ajax新手,我必须尽快将其转换。有什么指针吗?
示例代码:
<?php
$db = new SQLite3('some_db.db');
$query = $db->query('SELECT * FROM random_table');
?>
<table>
<th>
<td>ID</th><th>Value</th>
</th>
<?php
while ($row = $query->fetchArray (SQLITE3_ASSOC)) { ?>
<tr>
<td><?php echo $row['id'];?></td><td><?php echo $row['val'];?></td>
</tr>
<?php } ?>
</table>
答案 0 :(得分:1)
<pre>
JS CODE:
------------
function UpdateTable(){
$.ajax({
url: "script.php"
}).success(function(data) {
var d= JSON.parse(data);
$('table#someID tr').remove();
$('table#someID').append( d.tableContent );
});
}
$( document ).ready(function() {
setTimeout(UpdateTable(),60000);
});
PHP CODE: (script.php)
-----------
<?php
$res = array();
$res['result'] = false;
$db = new SQLite3('some_db.db');
$query = $db->query('SELECT * FROM random_table');
$res['tableContent'] = "";
while ($row = $query->fetchArray (SQLITE3_ASSOC)) {
$res['tableContent'] .= "<tr><td>".$row['id']."</td><td>".$row['val']."</td></tr>";
}
$res['result'] = true;
echo json_encode($res);
?>
</pre>