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



我的数据库中有一个表。我用while循环遍历它们。并用提取的数据创建一个HTMLdiv。我使用LIMIT 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中!!!!

尝试在谷歌上搜索一些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);

与工作代码

相比,此代码更具说明性

您可以将代码放在一个单独的文件中(例如: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

最新更新