Jump to content

Polling a Database for changes


Craig Hopson

Recommended Posts

ok there is probably a simple way to do this but i cant find it...I would like to auto update from mysql but only new entrys here is what i have index.php

<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script><script type="text/javascript">$(function() {	 setInterval(function() {	    $("<div/>").hide().load("inc/feedx.php", function() {	    $(this).prependTo("#foo").slideDown("slow");	    });	 }, 5000);});</script><div id="foo"></div>$result = mysql_query("SELECT e.*,f.* FROM emails as e left join files as f on f.email_id=e.id WHERE e.subject='1' ORDER BY e.date DESC LIMIT 20");while($row = mysql_fetch_array($result)){//somestuff}

feedx.php

$result = mysql_query("SELECT e.*,f.* FROM emails as e left join files as f on f.email_id=e.id WHERE e.subject='1' ORDER BY e.date DESC LIMIT 1");while($row = mysql_fetch_array($result)){//somestuff}

the code above works BUT it keeps adding the last SQL entry and "not just new ones"

Link to comment
Share on other sites

The PHP code should also return a timestamp that gives the time of the last time data was requested. The Javascript code should send that timestamp back to PHP, and PHP can use it to determine the last time data was loaded and find the data that has been added since then. All of the rows in the database will need timestamps so that you know which ones are the new ones.

Link to comment
Share on other sites

You get the current timestamp with the time function. PHP sends that to Javascript along with the rest of the data. Javascript saves the timestamp that it got from PHP. The next time it sends a request to PHP, it sends the timestamp also. PHP gets the timestamp and uses that as the last time that Javascript got data, and PHP returns everything since then. You don't want Javascript to generate the timestamp, it should just save the one it got from PHP and send it back.

Link to comment
Share on other sites

it's gonna take a little more time than just reading it. I would recommend practicing some of the code in the references as well, so you can get an understanding for how the code works. practice with functions, variables, etc. part of AJAX requires understanding the concept of callbacks as well. it's a little tricky, but is arguablly one of JS's most useful features.

  • Like 1
Link to comment
Share on other sites

ok i got it working using SESSION just fine but it twitches i think its the javascript when there is no update it starts to move then stops "twitching"lol

<script type="text/javascript">$(function() {	 setInterval(function() {	    $("<div/>").hide().load("inc/feedx.php", function() {	    $(this).prependTo("#foo").slideDown("slow");	    });	 }, 5000);});</script>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...