PHP Codeigniter - Ajax Infinite Scroll Pagination Example

By Hardik Savani | March 31, 2017 | | 5813 Viewer | Category : PHP Javascript jQuery Codeigniter Ajax

Share this post:

In this example, we are going to lean how to implement infinite scroll example in our codeigniter application.

Infinite Scroll is amazing feature if you have news latter page, posts page or any listing page that way we don't require to load more records at time and not need to load page every time. we sometimes require to use infinite scroll in our project or application. So in this post we learn how to make infinite scroll example from scratch.

I also provide demo for infinite scroll example, so you can see there. In this example we will create "posts" table and you have to add some manually dummy data. After successfully run this example you can see layout as like bellow:


Step 1: Download Fresh Codeigniter 3

In First step we will download fresh version of Codeigniter 3, so if you haven't download yet then download from here : Download Codeigniter 3.

Step 2: Create Database and Configuration

In this step we will create new database "test" and add new table "posts" in test database. You can use following SQL Query for create "posts" table. So let's create using bellow sql query:

item table:


`id` int(10) unsigned NOT NULL AUTO_INCREMENT,

`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,

`description` text COLLATE utf8_unicode_ci NOT NULL,



After create database and table successfully, we have to configuration of database in our Codeigniter 3 application, so open database.php file and add your database name, username and password.



defined('BASEPATH') OR exit('No direct script access allowed');

$active_group = 'default';

$query_builder = TRUE;

$db['default'] = array(

'dsn' => '',

'hostname' => 'localhost',

'username' => 'root',

'password' => 'root',

'database' => 'test',

'dbdriver' => 'mysqli',

'dbprefix' => '',

'pconnect' => FALSE,

'db_debug' => (ENVIRONMENT !== 'production'),

'cache_on' => FALSE,

'cachedir' => '',

'char_set' => 'utf8',

'dbcollat' => 'utf8_general_ci',

'swap_pre' => '',

'encrypt' => FALSE,

'compress' => FALSE,

'stricton' => FALSE,

'failover' => array(),

'save_queries' => TRUE


Step 3: Add Route

In this step you have to add one route in our route file. We will manage infinite scroll example on single route using jquery, so let's put route as bellow code:



defined('BASEPATH') OR exit('No direct script access allowed');

$route['default_controller'] = 'welcome';

$route['404_override'] = '';

$route['translate_uri_dashes'] = FALSE;

$route['my-post'] = 'AjaxController';

Step 4: Create Controller

Ok, now first we have to create one new controller AjaxController with index method. so create AjaxController.php file in this path application/controllers/AjaxController.php and put bellow code in this file:



defined('BASEPATH') OR exit('No direct script access allowed');

class AjaxController extends CI_Controller {

private $perPage = 5;


* Get All Data from this method.


* @return Response


public function index()



$count = $this->db->get('posts')->num_rows();


$start = ceil($this->input->get("page") * $this->perPage);

$query = $this->db->limit($start, $this->perPage)->get("posts");

$data['posts'] = $query->result();

$result = $this->load->view('data', $data);

echo json_encode($result);


$query = $this->db->limit(5, $this->perPage)->get("posts");

$data['posts'] = $query->result();

$this->load->view('myPost', $data);




Step 5: Create View Files

In this step, we will create two view files as listed bellow:

myPost.php: In this file, we will display layout with infinite ajax jquery.

data.php: In this file, we manage posts table data foreach, so we have to just manage posts data here.


<!DOCTYPE html>



<title>Codeigniter infinite scroll pagination</title>

<script src=""></script>

<link rel="stylesheet" href="">

<style type="text/css">


background: #e1e1e1;

padding: 10px 0px;

width: 100%;





<div class="container">

<h2 class="text-center">Codeigniter infinite scroll pagination</h2>


<div class="col-md-12" id="post-data">


$this->load->view('data', $posts);




<div class="ajax-load text-center" style="display:none">

<p><img src="">Loading More post</p>


<script type="text/javascript">

var page = 1;

$(window).scroll(function() {

if($(window).scrollTop() + $(window).height() >= $(document).height()) {





function loadMoreData(page){



url: '?page=' + page,

type: "get",

beforeSend: function()







if(data == " "){

$('.ajax-load').html("No more records found");






.fail(function(jqXHR, ajaxOptions, thrownError)


alert('server not responding...');







<?php foreach($posts as $post){ ?>


<h3><a href=""><?php echo $post->title ?></a></h3>

<p><?php echo $post->description ?></p>

<div class="text-right">

<button class="btn btn-success">Read More</button>


<hr style="margin-top:5px;">


<?php } ?>

Ok, now we are ready to run our Infinite Scroll example. Make sure you have some dummy records on posts table. So let's run bellow command on your root directory for quick run:

php -S localhost:8000

Now you can open bellow URL on your browser:


We are Recommending you: