views:

37

answers:

1

Hi at all, I have created a very simple blog at www.example.com with a only one page. When I connect to www.example.com I see all posts inserted in the database ( in mysql ).

Now I want that every 60 seconds an ajax request check in the database if there are new posts. If there are new posts these will be inserted at the top above the old posts.

This is my question:

How can I through Ajax retrieve only new posts ( and so distinguish old posts and new posts ) ?

+1  A: 

You should pass back an on-page counter than remembers the last post ID. Then

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

So on page

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;
    });
});
Andy
Your solution is good but if I disconnect internet for 1 hour ( for example ) and then reconnect it, I can't retrieve all new posts.I could save the date of last post in a cookies or in other place and then make ajax request using that date like this: SELECT required_fields, more_required, etc FROM posts WHERE post_date > post_savedIs this a good approach ? Where can I save the date of last post ?Thanks ;)
xRobot
updated for your scenario :)
Andy
thanks....... =D
xRobot