如何在特定时间间隔后使用AJAX和PhP从Postgresql数据库更新表

时间:2018-09-12 13:42:13

标签: php jquery ajax postgresql

我正在使用在此问题中完成的技术。 How to put data from database table to a html table using ajax and php

我的任务目标是:在特定时间间隔后,可以说从Postgresql数据库更新表,例如1分钟后。我要使用Ajax,因为我不希望该客户端参考页面。它应该自动完成。在应用ajax之前,我的代码在: estat hardware.php文件

<!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 < / p>

<?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更新。

<!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代码是:

<!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();

                           

1 个答案:

答案 0 :(得分:0)

将ajax方法放入函数中,每1分钟调用一次该函数。

<script>
setInterval(function() {
  UpdateHTMLTable();
}, 60000); // 60000 millisecond(60 second)
function UpdateHTMLTable(){
 //ajax code here
}
</script>