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

问题描述 投票:0回答:1

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

我的任务的目标是:在特定的时间间隔之后,在1分钟后更新来自postgresql数据库的表。我想使用Ajax因为我不想要那个客户端referesh页面。它应该自动完成。在应用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

<?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> 

我在客户端测试的ajax代码是否正确接收来自json的数据:

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

php jquery ajax postgresql
1个回答
0
投票

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

<script>
setInterval(function() {
  UpdateHTMLTable();
}, 60000); // 60000 millisecond(60 second)
function UpdateHTMLTable(){
 //ajax code here
}
</script>
© www.soinside.com 2019 - 2024. All rights reserved.