As of these days most social networking sites are using this event to load most recent posts every second without refreshing page, stream and tweets like Facebook, Google+ and Twitter, it loads in the top part of the page and I’d try developed one with jQuery and Ajax to mimic this functionality, it’s really simple and easy to use.
Okay let’s start.

Index.html

This contains html and javascript code. load('load_post.php');
XHTML
1
2
3
4
5
6
7
8
9
10
11
12
<script type=”text/javascript”>
   var autoLoad = setInterval(
   function ()
   {
      $(‘#load_post’).load(‘load_post.php’).fadeIn(“slow”);
   }, 10000); // refresh page every 10 seconds
</script>
<body>
    <!–append load-post.php echo value here–>
    <div id=”load_post”></div>
</body>

Load_post.php

This contains php script to get data from the database.
PHP
1
2
3
4
5
6
7
8
9
10
11
12
// include db configuration file
include_once(‘config.php’);
$sql = mysql_query( “SELECT * FROM tblpost LIMIT 0, 5″ ) or die(mysql_error().’ Error loading data.’);
$num_post = mysql_num_rows($sql); // count the number of rows
// check if there is data
if( $num_post ) {
   //this contains data to load into a page.
} else {
   echo ‘<p>There are no added post yet, please try to add to start sharing.</p>’;
}
This is very helpful if you want to get and load data from database in every x seconds without refreshing your page.

0 comments:

Post a Comment

 
Top