How can I slide in new posts from mysql table like Quora does using jquery?
-
I'm using jQuery to refresh posts from the database. Here's the jQuery: <script> jQuery(function($){ $('#loaddiv').show(); }) var auto_refresh = setInterval( function() { $('#loaddiv').hide().load('reload.php').show(); }, 5000); </script> And reload.php: <?php session_start(); ini_set('display_errors', 'On'); error_reporting(E_ALL); $mysql=mysql_connect('***','***','***'); mysql_select_db('jmtdy'); $result=mysql_query("select * from users where username='".$_SESSION['username']."'") or die(mysql_error()); $dbarray=mysql_fetch_assoc($result); $id=$dbarray['id']; $result1=mysql_query("select * from friendship where userid='".$id."'") or die(mysql_error()); $dbarray1=mysql_fetch_assoc($result1); $userid=$dbarray1['friendid']; $result2=mysql_query("select * from posts where userid='".$userid."' or userid='".$id."' ORDER BY id DESC LIMIT 20") or die(mysql_error()); if(mysql_num_rows($result2) >=1) { while($dbarray2=mysql_fetch_assoc($result2)) { echo '<div class="homestatus"><a class="homeusername" href="account.php?user='.$dbarray2['username'].'">'.$dbarray2['fname'].' '.$dbarray2['lname'].':<a><p class="homepost">'.$dbarray2['post'].'</p><p class="hometime">'.$dbarray2['time'].'</p></div>'; } } else { echo '<p class="homestatus">No new updates from your friends.</p>'; } ?> My question is, how can I slide in the new posts, and only the new posts, like Quora slides in new answers?
-
Answer:
Basically, you maintain state on the frontend. I'm assuming you have some kind of id to identify each post uniquely. In the markup, you embed the id of the posts displayed. You then extract the latest id from the markup and pass it as a parameter to reload.php, and use that to query the database (thus returning only "new posts"). And finally, you could use something like jQuery's append() or prepend() to add the new posts instead of what you're using currently.
Rohit Nair at Quora Visit the source
Other answers
Take a look at this: http://plugins.jquery.com/project/timers. It is a jQuery plugin which provides a bit more abstraction and convenience over setTimeout. If you want a bit more than that (a.k.a server push) then try comet :)
Phaneesh Nagaraja
Related Q & A:
- In Visual Studio 2012 or 2013, how can I register a DLL file on Windows 7 64-bit computer using a Custom Action command?Best solution by Stack Overflow
- How can I disable the new Yahoo popup?Best solution by malwaretips.com
- How can i create a new blog?Best solution by Yahoo! Answers
- How can I create a new font?Best solution by Yahoo! Answers
- How can I show the new photos I uploaded in flickr?
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.