Craig Hopson Posted November 14, 2012 Share Posted November 14, 2012 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 More sharing options...
justsomeguy Posted November 14, 2012 Share Posted November 14, 2012 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 More sharing options...
Craig Hopson Posted November 14, 2012 Author Share Posted November 14, 2012 the timestamp is there but how do i do that (Javascript) i can figure out the php Link to comment Share on other sites More sharing options...
justsomeguy Posted November 14, 2012 Share Posted November 14, 2012 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 More sharing options...
Craig Hopson Posted November 14, 2012 Author Share Posted November 14, 2012 i understand that how do i get javascript to receive the timestame Link to comment Share on other sites More sharing options...
justsomeguy Posted November 14, 2012 Share Posted November 14, 2012 It's part of the response that PHP sends back. I usually have my PHP respond with a JSON string when it's responding to ajax. You'll have to change the jQuery code, of course. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 14, 2012 Share Posted November 14, 2012 Another option would be the session, you can store the timestamp of the last request there and leave Javascript out of it. Link to comment Share on other sites More sharing options...
Craig Hopson Posted November 14, 2012 Author Share Posted November 14, 2012 that's the problem i don't understand javascript AT ALL i dont know why i read the tutorial like 3 times Link to comment Share on other sites More sharing options...
Craig Hopson Posted November 14, 2012 Author Share Posted November 14, 2012 Another option would be the session, you can store the timestamp of the last request there and leave Javascript out of it.oh yes i can do that thanks bud Link to comment Share on other sites More sharing options...
thescientist Posted November 14, 2012 Share Posted November 14, 2012 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. 1 Link to comment Share on other sites More sharing options...
Craig Hopson Posted November 14, 2012 Author Share Posted November 14, 2012 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 More sharing options...
justsomeguy Posted November 14, 2012 Share Posted November 14, 2012 You're always adding something to the document regardless of whether PHP returned anything. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now