PHP Table自动更新客户端

时间:2020-02-17 07:23:26

标签: php mysql

我正在尝试建立一个网站,以便每隔(x)秒用最新数据自动更新客户端上的表,此数据存储在数据库中并接收不断的更新。当前,查看最新数据的唯一方法是在网站上进行Ctrl + F5刷新。

任何帮助将不胜感激,代码如下:

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

1 个答案:

答案 0 :(得分:0)

您所处的最佳状态是使用EventSource。在https://developer.mozilla.org/en-US/docs/Web/API/EventSource中已通过一些示例对其进行了很好的描述。

EventSource实例打开与HTTP服务器的持久连接,该HTTP服务器以文本/事件流格式发送事件。

https://www.w3schools.com/html/html5_serversentevents.asp

处有另一个示例

我在这里使用了后端代码在一个循环中运行的场景,每个循环休眠几秒钟。循环的每次迭代都可以检测到是否有新数据可用,它将发送一些相应的事件消息,这些消息可以由前端收集。

https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events

提供了另一个很好的例子

我有一个警告。使用while(true){}样式循环时,如果客户端连接已关闭,则必须创建一种打破循环的方法。当您可以执行以下操作时...

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

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