In this tutorial, We will learn how ajax pagination in Codeigniter 3. I will teach you step-by-step to write step by step tutorial about Codeigniter ajax pagination.
Follow some steps:
Step 1:
Create posts table
CREATE TABLE IF NOT EXISTS `posts` (
`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,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=533 ;
then insert some dummy data to paginate it.
Step 2:
Make database configuration application/config/database.php
<?php
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 application/controllers/Post.php
<?php
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(){
parent::__construct();
$this->load->helper('url');
$this->load->library('pagination');
$this->load->database();
}
/**
* Get All Data from this method.
*
* @return Response
*/
public function index(){
$this->load->view('post_view');
}
/**
* 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>';
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['result'] = $users_record;
$data['row'] = $rowno;
echo json_encode($data);
}
}
Step 4:
Create View application/views/post_view.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Codeigniter 3 Ajax Pagination using Jquery Example</title>
<meta charset="utf-8">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />
<link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css">
<style type="text/css">
html, body { font-family: 'Raleway', sans-serif; }
a{ color: #007bff; font-weight: bold;}
</style>
</head>
<body>
<div class="container">
<div class="card">
<div class="card-header">
Codeigniter Ajax Pagination Example
</div>
<div class="card-body">
<!-- Posts List -->
<table class="table table-borderd" id='postsList'>
<thead>
<tr>
<th>S.no</th>
<th>Title</th>
</tr>
</thead>
<tbody></tbody>
</table>
<!-- Paginate -->
<div id='pagination'></div>
</div>
</div>
</div>
<!-- Script -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type='text/javascript'>
$(document).ready(function(){
$('#pagination').on('click','a',function(e){
e.preventDefault();
var pageno = $(this).attr('data-ci-pagination-page');
loadPagination(pageno);
});
loadPagination(0);
function loadPagination(pagno){
$.ajax({
url: '/post/loadRecord/'+pagno,
type: 'get',
dataType: 'json',
success: function(response){
$('#pagination').html(response.pagination);
createTable(response.result,response.row);
}
});
}
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;
sno+=1;
var tr = "<tr>";
tr += "<td>"+ sno +"</td>";
tr += "<td><a href='"+ link +"' target='_blank' >"+ title +"</a></td>";
tr += "</tr>";
$('#postsList tbody').append(tr);
}
}
});
</script>
</body>
</html>
Finally, You need to run your application
Thanks.......