Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.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/87.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 如何在每次while循环从数据库获取数据时插入一段代码_Php_Jquery_Ajax - Fatal编程技术网

Php 如何在每次while循环从数据库获取数据时插入一段代码

Php 如何在每次while循环从数据库获取数据时插入一段代码,php,jquery,ajax,Php,Jquery,Ajax,我的数据库中有一个表。我使用while循环遍历它们。并用获取的数据生成一个HTML div。我使用了限制10,因为帖子中的条目会随着用户不断将帖子插入表中而不断变化 $get_ids=mysql_query("SELECT * FROM posts ORDER BY id LIMIT 10"); while($row = mysql_fetch_array($get_ids)){ $sm=$row['message']; echo "<div

我的数据库中有一个表。我使用while循环遍历它们。并用获取的数据生成一个HTML div。我使用了限制10,因为帖子中的条目会随着用户不断将帖子插入表中而不断变化

$get_ids=mysql_query("SELECT * FROM posts ORDER BY id LIMIT 10");
while($row = mysql_fetch_array($get_ids)){
            $sm=$row['message'];
            echo "<div>".$sm"</div>";
}

我想知道的是如何使用jquery让这个脚本每隔1秒左右将这10个div插入到我的DOM中。请紧急帮助

您应该将该代码放在一个单独的文件中,例如:divfill.php,然后使用类似的内容

$.get({
    'url': 'divfill.php',
    'success': function(data) {
       $("#content").html($("#content").html() + data);
    }
});

在我的例子中,我在我的js文件中添加了以下方法

    //call the target function after 250 ms
function async(targetFn, callback) {
    setTimeout(function () {
        targetFn();
        callback();
    }, 250);
}
我调用这个函数,如下所示

async(BindValuesInLookup, function () {
         BuildGrid()
         GetInfo();
    });
BindValuesInLookup是目标函数。它在250毫秒后被发射。完成后,将执行回调函数。因此,您可以在循环中使用此功能,并将超时时间增加到1000 1秒

谢谢,
Vim

尝试在谷歌上搜索一些jquery和php Web服务示例。基本上你应该这样做:

//Javascript function which fetches the single data as J.D.Smith suggested
function getHtml()
{
   $.get({
      'url': 'Address of your php webservice',
      'success': function(data) {
           $("#content").html($("#content").html() + data);   //Append html to your page
       }
   });
}

//Call the function every 10 sec, place it in $(document).ready() or anything else you use
window.setTimeout(function () {
        getHtml();
    }, 10000);

这段代码更像是一个示例,而不是工作代码。这里提供了很好的文档和许多示例。您能否解释一下整个jquery函数PHP代码只运行一次:当服务器将页面发送到客户端时。从理论上讲,可以使页面持续加载。但是,您只能将代码添加到HTML文件的底部。它有用吗?是的,但不是很好。我强烈建议不要尝试这样的事情,而是使用AJAX。好的,我打算使用AJAX。这基本上就是我要问的。既然我不是jquery大师,你能给我一个提示或一个简短的片段,告诉我这应该如何实现吗?把我上面的代码放在一个函数中,用你需要的任何标识符替换内容。然后,使用每1秒运行一次函数。函数getposts{$.get{'url':'php/sortest.php','success':函数数据{$.post container.html$.post-container.html+data;}}}};}window.setIntervalgetposts,1000;但不管用这对我来说似乎应该管用,尽管我以前从未尝试过,所以我不能肯定。