简体   繁体   English

PHP Table 自动更新客户端

[英]PHP Table auto update client side

I am trying to get a website to auto update the table on client side every (x) seconds with the newest data, this data is stored in a database and receives constant updates.我试图让一个网站每 (x) 秒用最新数据自动更新客户端的表,这些数据存储在数据库中并接收不断更新。 Currently the only way to see the newest data is to do a Ctrl+F5 refresh on the website.目前查看最新数据的唯一方法是在网站上按 Ctrl+F5 刷新。

Any assistance will be greatly appreciated, code is as follows:任何帮助将不胜感激,代码如下:

    include ('./vam_index_header.php');
    include ('./helpers/conversions.php');
    header('Cache-Control: no-cache');
    header('Pragma: no-cache');
    if (!isset($_GET["page"]) || trim($_GET["page"]) == "") {
        ?>
        <?php
            $sql = 'select callsign, arrival, departure, flight_status, name, surname, pending_nm, plane_type from vam_live_flights vf, gvausers gu where gu.gvauser_id = vf.gvauser_id ';
            if (!$result = $db->query($sql)) {
                die('There was an error running the query [' . $db->error . ']');
            }
            $row_cnt = $result->num_rows;
            $sql = "SELECT flight_id FROM `vam_live_flights` WHERE UNIX_TIMESTAMP (now())-UNIX_TIMESTAMP (last_update)>180";
            if (!$result = $db->query($sql)) {
                die('There was an error running the query [' . $db->error . ']');
            }
            while ($row = $result->fetch_assoc())
            {
                $sql_inner = "delete from vam_live_acars where flight_id='".$row["flight_id"]."'";
                if (!$result_acars = $db->query($sql_inner))
                {
                die('There was an error running the query [' . $db->error . ']');
                }
                $sql_inner = "delete from vam_live_flights where flight_id='".$row["flight_id"]."'";
                if (!$result_acars = $db->query($sql_inner))
                {
                die('There was an error running the query [' . $db->error . ']');
                }
            }
            if ($row_cnt>0){
        ?>
        <div class="row" id="live_flights">
            <div class="col-md-12">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><IMG src="images/icons/ic_flight_takeoff_white_18dp_1x.png">&nbsp;<?php echo "LIVE FIGHTS" ?></h3>
                    </div>
                    <div class="panel-body">
                        <div class="table-responsive">
                            <table class="table table-hover" id="live_flights_table">
                            <?php
                                    echo "<tr><th>" . LF_CALLSIG . "</th><th>" . LF_PILOT . "</th><th>" . LF_DEPARTURE . "</th><th>" . LF_ARRIVAL . "</th><th>" . FLIGHT_STAGE . "</th><th>". BOOK_ROUTE_ARICRAFT_TYPE . "</th><th>" . PERC_DONE ."</th><th>" . PENDING_NM . "</th></tr>";
                            ?>
                            </table>
                        <?php include ('./vam_live_flights_map.php') ?>
                    </div>
                </div>
            </div>
        </div>```

The best in your situation could be to use the EventSource .在您的情况下最好的方法是使用EventSource It's well described at https://developer.mozilla.org/en-US/docs/Web/API/EventSource with some examples.它在https://developer.mozilla.org/en-US/docs/Web/API/EventSource 中有很好的描述,并附有一些示例。

An EventSource instance opens a persistent connection to an HTTP server, which sends events in text/event-stream format. EventSource 实例打开与 HTTP 服务器的持久连接,该服务器以文本/事件流格式发送事件。

There is another example at https://www.w3schools.com/html/html5_serversentevents.asp https://www.w3schools.com/html/html5_serversentevents.asp还有一个例子

I have used there in a scenario where the backend code run in a loop that sleeps for a few seconds per loop.我在一个场景中使用了那里,后端代码在一个循环中运行,每个循环休眠几秒钟。 And each iteration of the loop can detect if new data is available and it will send some corresponding event messages that can be collected by the front-end.并且循环的每次迭代都可以检测是否有新数据可用,并且会发送一些相应的事件消息,这些消息可以由前端收集。

Another good example is available at https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events另一个很好的例子是https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events

I have one word of caution.我有一个警告。 When using the while(true){} style loop, you have to create a way to break the loop if the client-side connection is closed.使用while(true){}样式循环时,您必须创建一种方法来在客户端连接关闭时中断循环。 That when you can do something like the following ...当您可以执行以下操作时...

<?php
while(true) {
    //... some code

    // A way to break out of the loop.
    if (connection_aborted()) {
        break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM