Codeigniter Ajax Pagination using JQuery Example

By Hardik Savani November 5, 2023 Category : PHP jQuery MySql Codeigniter Ajax

Are you looking for ajax pagination in codeigniter 3 app?, If yes then here i write step by step tutorial about php codeigniter ajax pagination from starch.

As we know, we love jquery and everyone wants to add jquery ajax code on his site. Because it saves time and ignores too much loading. without jquery ajax you have to load every time page, but if you use jquery ajax then it just loads data without loading the whole page.

So in this example, I created the "posts" table and then add some dummy records on it. Then display all the data using ajax pagination. You have to just follow a few steps and then you will get a full example like as below screenshot.


Step 1: Create posts Table

In first table we must have one table with some dummy records. For this example i will create "posts" table and dummy records as like bellow query:

posts table


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

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

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



Step 2: Make database configuration

in second step, we will add database details like username, password and database name. so you can do it from here.



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: Create Post Controller

In this step, we will create Post Controller with index() and loadRecord(). index method will return view only, loadRecord() will get ajax data.

So, create new method on this controllers folder and put bellow code:



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

class Post extends CI_Controller {


* Get All Data from this method.


* @return Response


public function __construct(){







* Get All Data from this method.


* @return Response


public function index(){




* Get All Data from this method.


* @return Response


public function loadRecord($rowno=0){

$rowperpage = 5;

if($rowno != 0){

$rowno = ($rowno-1) * $rowperpage;


$allcount = $this->db->count_all('posts');

$this->db->limit($rowperpage, $rowno);

$users_record = $this->db->get('posts')->result_array();

$config['base_url'] = base_url().'post/loadRecord';

$config['use_page_numbers'] = TRUE;

$config['total_rows'] = $allcount;

$config['per_page'] = $rowperpage;

$config['full_tag_open'] = '<div class="pagging text-center"><nav><ul class="pagination">';

$config['full_tag_close'] = '</ul></nav></div>';

$config['num_tag_open'] = '<li class="page-item"><span class="page-link">';

$config['num_tag_close'] = '</span></li>';

$config['cur_tag_open'] = '<li class="page-item active"><span class="page-link">';

$config['cur_tag_close'] = '<span class="sr-only">(current)</span></span></li>';

$config['next_tag_open'] = '<li class="page-item"><span class="page-link">';

$config['next_tag_close'] = '<span aria-hidden="true"></span></span></li>';

$config['prev_tag_open'] = '<li class="page-item"><span class="page-link">';

$config['prev_tag_close'] = '</span></li>';

$config['first_tag_open'] = '<li class="page-item"><span class="page-link">';

$config['first_tag_close'] = '</span></li>';

$config['last_tag_open'] = '<li class="page-item"><span class="page-link">';

$config['last_tag_close'] = '</span></li>';


$data['pagination'] = $this->pagination->create_links();

$data['result'] = $users_record;

$data['row'] = $rowno;

echo json_encode($data);



Step 4: Create View File

In last step, we have to create view file, we will create new view file "post_view.php" on views folder and put bellow code on that file.


<!DOCTYPE html>

<html lang="en">


<title>Codeigniter 3 Ajax Pagination using Jquery Example -</title>

<meta charset="utf-8">

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

<link href=",600" rel="stylesheet" type="text/css">

<style type="text/css">

html, body { font-family: 'Raleway', sans-serif; }

a{ color: #007bff; font-weight: bold;}




<div class="container">

<div class="card">

<div class="card-header">

Codeigniter Ajax Pagination Example -


<div class="card-body">

<!-- Posts List -->

<table class="table table-borderd" id='postsList'>









<!-- Paginate -->

<div id='pagination'></div>




<!-- Script -->

<script src=""></script>

<script type='text/javascript'>




var pageno = $(this).attr('data-ci-pagination-page');




function loadPagination(pagno){


url: '/post/loadRecord/'+pagno,

type: 'get',

dataType: 'json',

success: function(response){






function createTable(result,sno){

sno = Number(sno);

$('#postsList tbody').empty();

for(index in result){

var id = result[index].id;

var title = result[index].title;

var content = result[index].slug;

content = content.substr(0, 60) + " ...";

var link = result[index].slug;


var tr = "<tr>";

tr += "<td>"+ sno +"</td>";

tr += "<td><a href='"+ link +"' target='_blank' >"+ title +"</a></td>";

tr += "</tr>";

$('#postsList tbody').append(tr);







Now it's all done steps. You run and check it...

I hope it can help you...