我在www.example.com上创建了一个非常简单的博客,只有一页。
当我连接到www.example.com时,我看到数据库中(在mysql中)插入的所有帖子。

现在,我希望ajax请求每60秒应在数据库中检查一次是否有新帖子。如果有新职位,这些职位将插入旧职位上方的顶部。

这是我的问题:

我如何通过Ajax仅检索新帖子(从而区分旧帖子和新帖子)?

最佳答案

您应该传回页面计数器,以记住上一个帖子ID。然后

 SELECT required_fields, more_required, etc
 FROM posts
 WHERE post_id > on_page_variable


所以在页面上

var last_post_id = 32; // or whatever
$.getJSON('script.php', {'post_id': last_post_id }, function(data){
    $.each(data.post, function(post){
         $('div.post:first').before(post.html);
         last_post_id = post.id;
    });
});

关于php - Ajax:区分旧帖子和新帖子,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2454974/

10-09 08:15
查看更多