Load the data While Scroll Down

Load the data While Scroll Down:

Load Data or Contents While Scrolling Page Down with Jquery, Now a days you can see many web sites loading page data as user scroll down. This is very easy and useful trick to load images as user scroll using jquery.

load the data while scroll down
load the data while scroll down




Loading Database records automatically when user scrolls down to the bottom of the page. This technique can be seen in Twitter, facebook, and several other websites.

In this scroll down script i am using php and mysql, Jquery.

Database file:

Database Name : tse

Table structure: 

CREATE TABLE IF NOT EXISTS `messages` (
`mes_id` int(11) NOT NULL,
  `msg` varchar(250) DEFAULT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=119 ;

load_data.php:

<?php
include('config.php');
$last_msg_id=$_GET['last_msg_id'];
$action=$_GET['action'];
if($action <> "get")
{
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Load Data while scrolling san web tutorials</title>
<link rel="stylesheet" href="san.css" type="text/css" />
<script type="text/javascript" src="jquery-1.2.6.pack.js"></script>
<script type="text/javascript">
$(document).ready(function(){
function last_msg_funtion() 
    var ID=$(".message_box:last").attr("id");
$('div#last_msg_loader').html('<img src="bigLoader.gif">');
$.post("load_data.php?action=get&last_msg_id="+ID,
function(data){
if (data != "") {
$(".message_box:last").after(data);
}
$('div#last_msg_loader').empty();
});
};  
$(window).scroll(function(){
if  ($(window).scrollTop() == $(document).height() - $(window).height()){
  last_msg_funtion();
}
}); 
});
</script>
</head>
<body>
<div align="center">
<div>
<h2><a href="http://sanwebtutorials.blogspot.in/">sanwebtutorials.blogspot.in</a></h2>
</div>
<?php
include('load_first.php');
?>
<div id="last_msg_loader"></div>
</div>
</body>
</html>

<?php
}
else
{
include('load_second.php');
}
?>

config.php:

<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "tse";
$prefix = "";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");
?>

load_fist.php:

<?php
$sql=mysql_query("SELECT * FROM messages ORDER BY mes_id DESC LIMIT 10");
while($row=mysql_fetch_array($sql))
{
$msgID= $row['mes_id'];
$msg= $row['msg'];
?>
<div id="<?php echo $msgID; ?>"  align="left" class="message_box" style="margin-top:25px;" >
<?php echo $msg;  ?> 
</div>
<?php
}
?>

load_second.php:

<?php
$last_msg_id=$_GET['last_msg_id'];
 $sql=mysql_query("SELECT * FROM messages WHERE mes_id < '$last_msg_id' ORDER BY mes_id DESC LIMIT 5");
 $last_msg_id="";
while($row=mysql_fetch_array($sql))
{
$msgID= $row['mes_id'];
$msg= $row['msg'];
?>
<div id="<?php echo $msgID; ?>"  align="left" class="message_box"  style="margin-top:25px;">
<?php echo $msg; ?> 
</div>
<?php
}
?>



Post a Comment

0 Comments