Berbagi itu indah

Codeigniter Load More Record on Button Click

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,

  PRIMARY KEY (`id`)


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.


	'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:


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:



     $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

n 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.


	Codeigniter infinite scroll pagination




Codeigniter infinite scroll pagination

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


title ?>

description ?>

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: