Auto Load Dynamic Data with jquery smooth scroll


When you have millon of data in your database how will you display that data to your website visitor? Either you can display the data using pagination or auto load the data through jquery scrolling. In this article I will show you how to load dynamic data from your database while you scroll down your mouse or press bottom arrow key on your keyboard. Now a days many ecommerce websites are using jquery scrolling method to load their data.

Below are the table structure of user table.

   uid int(11) NOT NULL AUTO_INCREMENT,  
   user_name varchar(100) NOT NULL,
   email varchar(50) NOT NULL,
   contact_number varchar(15) NOT NULL,
   user_bio text NOT NULL,
   PRIMARY KEY (uid)

DB Configuration
Below are the database configuration files.Create a file name as config.php and paste below code in it.

define("SHOW_RESULT_PER_PAGE",50); //How many records you want to show per scrolling
define("DB_HOST_NAME","your host name");//Replace with your host name
define("DB_USER_NAME","your db username");//Replace with your database username
define("DB_PASSWORD","your db password");//Replace with your database password
define("DB_NAME","your database name");//Replace with your database name

// Check whether connection is successfully established or not
if (mysqli_connect_errno()){
  	die("Unable to connect to Database: " . mysqli_connect_error());

Jquery Scrolling Code
Create a javascript file and name as scroll.js and paste below code.

var current_pos = 0; //This variable will store How many times record loaded
var busy  = false; //This global variable will prevents from multipal ajax Load
function AutoLoadDataWithScroll(total_records,url,show_result_span_id){
	//Initially call this function to load records
	/*This will load the page while you scroll your page*/
	$(window).scroll(function() { 
		if($(window).scrollTop() + $(window).height() == $(document).height()) //if user has reached bottom of the page

/*Loading Data*/
function LoadData(total_records,url,show_result_span_id){
	/*If current_pos is less than or equal to total_page and busy variable is false then execute below code*/
	if(current_pos <= total_records && busy==false)
		busy = true; //Again prevent multiple ajax loading
		$('#loading').html('Please wait...'); //show loading text
		$.ajax({method: "POST",
			    url: url,
			    data: { start_from:current_pos}
		.done(function( response ) {
				//hide loading image
				$('#loading').html(''); //hide loading text once data is received
				current_pos++; //Increase to next position
			    busy = false; 
		.fail(function(xhr,status,err) { //If there is any error in ajax then this will execute and display error message
				$('#loading').html(''); //hide loading image
				loading = false;

In the above jquery file there are two functions AutoLoadDataWithScroll() and LoadData(). Both function will accept 3 parameters i.e total_records,url and show_result_span_id. total_records should be total pages(total record in your database/how many record show per page). url should be the PHP file which will retrieve data from database and finally show_result_span_id is the unique id which will display the result. LoadData() function simply send HTTP request to web server and retrieve data from the server then display the data to the user.

Lets describe little bit about AutoLoadDataWithScroll() function. The window scroll in jquery i.e $(window).scroll applies to window object when user scroll down to a different location. scrollTop() which returns the current vertical position of the scroll bar. scrollTop() returns 0 if scroll bar is at the top or if the element is not scrollable. $(window).height() which returns the height of the window whereas $(document).height() returns the height of the document.

In this file there is one function which checks whether there are records in user table or not.

define("SHOW_RESULT_PER_PAGE",50); //How many records you want to show per scrolling
/*This function will calculate how many records are there in your table*/
function TotalRecord(){
	global $connection;
	$sql="SELECT uid FROM user";
	return mysqli_num_rows($result);

Add some css in the listing.

body{margin: 0px;padding:0px;}
.main{width:500px;margin:0 auto;}

	border:1px solid #999;
.data-listing th{
font-size: 18px;
.data-listing td{padding:5px;}
.data-listing td.no_record{
	color: #333;
    font-weight: bold;
    text-align: center;


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Auto Scroll</title>
<link rel="stylesheet" href="style.css">
<script type="text/javascript" src=""></script>
<script type="text/javascript" src="scroll.js"></script>
<div class="main">
  <table class="data-listing" border="0" cellpadding="0" cellspacing="0">
        <th width="25%">User Name</th>
        <th width="25%">Email Address</th>
        <th width="25%">Contact Number</th>
        <th width="25%">User Biography</th>
    <tbody id="load_result">
    <script type="text/javascript">
		AutoLoadDataWithScroll('<?php echo $TotalPages;?>','get_result.php','load_result');

This will retrieve the data from database.

	//get current starting position of records
	$start_from=($start * $limit);
	$sql="SELECT * FROM user LIMIT $start_from,$limit";
		echo '<tr>
		echo '<tr><td colspan="4" class="no_record">No more records.</td></tr>';

Leave a comment


Enter your email address to subscribe to this blog and receive notifications of new posts by email.