Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在HTML表中追加新行,而不重新编辑整个页面_Php_Jquery_Ajax - Fatal编程技术网

Php 在HTML表中追加新行,而不重新编辑整个页面

Php 在HTML表中追加新行,而不重新编辑整个页面,php,jquery,ajax,Php,Jquery,Ajax,我有一个管理门户,我们可以看到有多少用户注册。数据存储在MySQL中 当mySQL表中有新数据可用时,我需要自动追加一个新行,而无需重新加载整个页面 <table> <tr> <th>NAME</th> </th>Register Time </th> </tr> <?php while($row=mysql_fetch_array(mysql_que

我有一个管理门户,我们可以看到有多少用户注册。数据存储在MySQL中

当mySQL表中有新数据可用时,我需要自动追加一个新行,而无需重新加载整个页面

<table>
    <tr>
    <th>NAME</th>
    </th>Register Time </th>
     </tr>

     <?php
     while($row=mysql_fetch_array(mysql_query("SELECT * FROM `register`"))){
         echo '<tr>';
         echo '<td>'.$row['name'].'</td>';
          echo '<td>'.$row['reg_time'].'</td>';
          echo '</tr>';
     }
     echo '</table>';

名称
注册时间

不要使用
mysql.*
函数。改用
mysqli.*

结果页面:

<table class="users">
    <tr>
        <th>NAME</th>
        </th>Register Time </th>
    </tr>

<?php

$query = mysql_query("SELECT * FROM `register`");
$lastId = 0;
while ($row = mysql_fetch_array($query)) {
    $lastId = $row['id'];
    echo '<tr>';
    echo '<td>'.$row['name'].'</td>';
    echo '<td>'.$row['reg_time'].'</td>';
    echo '</tr>';
}
echo '</table>';
echo '<script>var lastId = '. $lastId .'</script>'; // remember the last id

?>
$(document).ready(function() {
    var usersTable = $(".users");

    setInterval(function() {
        $.ajax({
            url: 'get_users.php?id='+lastId,
            dataType: 'json',
            success: function(json) {
                if (json.length > 0) {
                    $.each(json, function(key, user) {
                        usersTable.append('<tr><td>'+user.name+'</td><td>'+user.reg_time+'</td></tr>');
                        lastId = user.id;
                    });
                }
            }
        });
    }, 10000);
});
$lastId = (int)$_GET['id'];

$query = mysql_query('SELECT * FROM `register` WHERE `id` >' . $lastId);
$result = array();

while ($row = mysql_fetch_array($query)) {
    $result[] = $row;
}

echo json_encode($result);
主要的一点是每X秒进行一次ajax调用,并附加在当前页面上最后一个用户之后注册的所有用户

全文如下:


为了实现这一目标,您做了哪些尝试?你的尝试失败了吗?也许这会有帮助:你尝试了什么?通常,这个社区希望看到一些初步的问题解决工作,而不是直接的“请从步骤0开始为我编写此代码”我使用META标记刷新页面的整个内容,但我需要一些不需要任何重新加载的东西@jamesmontagenits实际上不起作用echo'var lastId='$行['id'].';//记住最后一个id并没有得到最后一个id,因为它超出了循环,而我给了你们一个简单的例子,当然我并没有测试它。不管怎样,修正了,现在看看我的答案。