1

Possible Duplicate:
jquery/ajax load new content when available

i have a table named news with 3 fields i.e (id, news, time) and i have a setInterval after every 3mints to fetch news from google or any news site ....

now i want to fetch only new rows inserted after every 5 minutes...with jquery $.ajax()...how can i do that...

do i reload the whole table or there is a way to fetch only the new ones...

1

2 Answers 2

2

Have a variable which stores the latest ID number for a returned row, and then include that in the AJAX call (having the PHP server-side script only return rows with an ID greater than it).

In the Calling Page

var latestID = 0;
...
/* Within the Variables Section for the AJAX Call */
  laterThan : latestID
...
/* Within the Success AJAX Function */
  latestID = data.highestID; /* Where "data.highestID" is the highest ID returned */

In the Server-Side Script being called via AJAX

<?php
...
/* Within the SQL Query creating the returned rows */
'WHERE
  ...
  AND `id` > '.( isset( $_GET['latestID'] ) ? (int) $_GET['latestID'] : 0 )
2

You'll want to select when your time is greater than current time - 3 minutes and current time. This will give you the newest rows that have been inserted within the last three minutes.

I don't know what database you're running, but it will be much faster to run a query every three minutes than try to process over the entire database result set.

Not the answer you're looking for? Browse other questions tagged or ask your own question.