Load Infinite Data while Scrolling Page Down in jQuery, AJAX ,PHP AND MYSQL

Sharing is caring!

This excercise about my top choice put Dzone like information stacking while page scrolling down with jquery and Php. We have heaps of information however can’t show all. This script encourages you to showcase small information and make quicker your site. Investigate live demo and span down.

Database Table
CREATE TABLE messages(
mes_id INT PRIMARY KEY AUTO_INCREMENT,
msg TEXT);
Browser Capability : Safari, Firefox, IE, Chrome
load_data.php

When we are scrolling down a webpage, the script($(window).scroll) finds that you are at the bottom and calls the last_msg_funtion(). Take a look at $.post(“”) eg:$.post(“load_data.php?action=get&last_msg_id=35”)

<?php
include(‘config.php’);
$last_msg_id=$_GET[‘last_msg_id’];
$action=$_GET[‘action’];

if($action <> “get”)
{
?>

http://jquery.js

$(document).ready(function()
{
function last_msg_funtion()
{
var ID=$(“.message_box:last”).attr(“id”);
$(‘div#last_msg_loader’).html(‘‘);
$.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();
}
});
});

</head>
<body>
<?php
include(‘load_first.php’); //Include load_first.php
?>

</body>
</html>
<?php
}

else
{
include(‘load_second.php’); //include load_second.php
}
?>

load_first.php
Contains PHP code to load 20 rows form the message table.
<?php
$sql=mysql_query(“SELECT * FROM messages ORDER BY mes_id DESC LIMIT 20”);
while($row=mysql_fetch_array($sql))
{
$msgID= $row[‘mes_id’];
$msg= $row[‘msg’];
?>

” class=”message_box” >

<?php
}
?>
load_second.php
Contains PHP code to load 5 rows less than last_msg_id form the message table.

<?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’];
?>

” class=”message_box” >

<?php
}
?>

Download Full Code

7 thoughts on “Load Infinite Data while Scrolling Page Down in jQuery, AJAX ,PHP AND MYSQL

Leave a Reply

Your email address will not be published. Required fields are marked *

Got Project on mind? Let's ConnectContact Us

Secured By miniOrange