如何在特定时间间隔后使用AJAX和PhP从Postgresql数据库更新表 [英] How to update table from postgresql database after specefic time interval using AJAX and PhP
问题描述
I am using the technique done in this question. How to put data from database table to a html table using ajax and php.
我的任务目标是:在特定时间间隔后,可以说从Postgresql数据库中更新表,例如1分钟后.我要使用Ajax,因为我不希望该客户端参考页面.它应该自动完成.在应用ajax之前,我的代码在: estat hardware.php文件
Goal of my task is: update table from postgresql database after specific time interval lets say, after 1 minute. I want to use Ajax because I don't want that client referesh page. It should be done automatically. Before apply ajax, my code is in: estat hardware.php file
<!DOCTYPE html>
<html>
<head>
<style> <?php include './../css/estat_hardware.css'; ?> </style>
</head>
<?php
$host = "10.80.145.98";
$port ="5432";
$user = "postgres";
$pass = "AIRFI2014";
$db = "hospital";
?>
<body>
<div class="maindiv">
<div id="tagstable">
<h3>Estat Tags</h3>
<?php
$con = pg_connect("host=$host dbname=$db user=$user password=$pass") or die ("Could not connect to server\n");
$query = "SELECT * FROM beacon";
$result = pg_query($con, $query) or die("Cannot execute query: $query\n");
$i = 0;
echo '<html><body><table>';
echo '<th>' . "TAG" . '</td>';
echo '<th>' . "BATERIA" . '</td>';
echo '<th>' . " VIST ÚLTIMA COP " . '</td>';
echo '<th>' . "ESTAT" . '</td>';
$i = 0;
while ($row = pg_fetch_array($result)){
$estat="TODO";
echo "<tr>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$estat."</td>";
echo "</tr>";
}
pg_free_result($result);
echo '</table></body></html>';
pg_close($con);
?>
</div>
</div>
</body>
</html>
我想使用AJAX更新同一张表.因此,我研究了有关Ajax的代码,并编码了调用服务器功能的机制,服务器将从数据库中获取数据并使用json将其返回.服务器文件为: estat_hardware_server.php
I want to update the same table using AJAX. So, I studied about ajax and coded the mechanism in which there will be a call to server function, the server will get data from database and resturn it using json.The server file is: estat_hardware_server.php
<?php
$host = "10.80.145.98";
$port ="5432";
$user = "postgres";
$pass = "AIRFI2014";
$db = "hospital";
$con = pg_connect("host=$host dbname=$db user=$user password=$pass") or die ("Could not connect to server\n");
$query = "SELECT mac,ts,battery FROM beacon";
$result = pg_query($con, $query) or die("Cannot execute query: $query\n");
if(pg_num_rows($result)){
$data=array();
while($row=pg_fetch_array($result)){
$data[] = array(
'mac'=>$row['mac'],
'ts' =>$row['ts'],
'battery'=>$row['battery']
);
}
echo json_encode($data);
pg_free_result($result);
pg_close($con);
}
?>
在客户端,我有以下代码应从ajax更新.
On client side, I have the following code which should be updated from the ajax.
<!DOCTYPE html>
<html>
<head>
<style> <?php include './../css/estat_hardware.css'; ?> </style>
</head>
<body>
<div class="maindiv">
<div id="tagstable">
<h3>Estat Tags</h3>
<?php
echo '<html><body><table>';
echo '<th>' . "TAG" . '</td>';
echo '<th>' . "BATERIA" . '</td>';
echo '<th>' . " VIST ÚLTIMA COP " . '</td>';
echo '<th>' . "ESTAT" . '</td>';
echo '</table></body></html>';
?>
</div>
</div>
</body>
</html>
我在客户端测试以正确接收来自json的数据的ajax代码是:
My ajax code which I tested on client side for correctly receiving data from json is:
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myObj = JSON.parse(this.responseText);
for (i = 0; i < myObj.length; i++) {
// I need to update the table here, after time interval 1 minute.
}
}
};
xmlhttp.open("GET", "pages/estat_hardware_server.php", true);
xmlhttp.send();
推荐答案
将ajax方法放入函数中,每1分钟调用一次该函数.
put ajax method into a function, call that function every 1 minute.
<script>
setInterval(function() {
UpdateHTMLTable();
}, 60000); // 60000 millisecond(60 second)
function UpdateHTMLTable(){
//ajax code here
}
</script>
这篇关于如何在特定时间间隔后使用AJAX和PhP从Postgresql数据库更新表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!