how to create custom pagination in codeigniter

Today, We want to share with you pagination in codeigniter.In this post we will show you pagination in codeigniter using ajax, hear for pagination in codeigniter 4 we will give you demo and example for implement.In this post, we will learn about PHP Codeigniter Load Data While Ajax Infinite Scrolling Pagination with an example.

Codeigniter Pagination Class Library

Load migration class library

$this->load->library('pagination');

1. Hiding the pages.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class pagination_controller extends CI_Controller 
{
public function pagehide()
{
$this->load->library('pagination');
$config['base_url'] = 'http://pakainfo.com/index.php/test/page/';
$config['total_rows'] = 200;
$config['per_page'] = 20;
$config['display_pages'] = false;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
}
}
?>

2. Adding attribute to anchors.

$config['attributes'] = array('class' => 'myclass');

3. Disabling the “rel” attribute.

$config['attributes']['rel'] = FALSE;

1. Initialize.

initialize([$params = array()])

2. Create links.

create_links()

Full example of pagination.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class pagination_controller extends CI_Controller 
{
public function paginationLink()
{
$this->load->library('pagination');
$config['base_url'] = 'http://pakainfo.com/index.php/test/page/';
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
}
}
?>

More Example About Pagination

<div class="row"> 
<div class="col-md-3 col-sm-offset-4">
<?php  
echo $this->session->flashdata('message'); 
?>
<form name="custom_post" onsubmit="return validateForm()">
<?php
if(isset($_GET['message']) && ($_GET['message'] == 1))
{
echo "<p class='sucess'><b>Insert Member sucessfull</b></p>";
}
?> 
<div><p id="error"> </p></div>
<form method="post" action="<?php echo base_url(); ?>index.php/MemberController/membersdatalist">
<div class="form-group">
<input type="text" name= "search" class="form-control" placeholder="Name, MemberName And Email">
<button class="btn btn-info">Search Now</button>
<a href="<?php echo base_url('index.php/MemberController/register/');?>" class="btn btn-info");">Add Members</a>
</div></div></div>
<div class="row"><div class="col-md-14"><div class="table-responsive"><table class="table table-bordered">
<thead>
<th>Name</th><th>Member Name</th><th>Email Id</th><th>Contact Number</th>
<th>Date Of Birth</th><th>Country</th><th>State</th><th>City</th>
<th>Upload File</th><th>Edit Action</th><th>Delete Action</th>
</thead>
 <?php if(!empty($data)){?>
<?php foreach ($data as $row)
 {?>
<tr> <td><?php echo $row['name'] ?></td><td><?php echo $row['uname'] ?></td>
<td><?php echo $row['email'] ?></td><td><?php echo $row['mob'] ?></td>
<td><?php echo $row['date_of_birth'] ?></td>
<td><?php echo getCountryByCode($row['country']) ?></td>
<td><?php echo $row['state'] ?></td>
<td><?php echo $row['city'] ?></td>
<td><?php echo $row['fileToUpload'] ?></td>
<td><a href="<?php echo base_url('index.php/MemberController/editMember/'.$row['id']);?>" class="btn btn-primary" onclick="return confirm('Are you sure you want to Update this product?');">Edit</a></td>
<td><a href="<?php echo base_url('index.php/MemberController/deleteAction/'.$row['id']);?>" class="btn btn-warning" onclick="return confirm('Are you sure you want to Delete this product?');">Delete</a></td></tr>
<?php
}
}
 ?>
</table>
<?php echo $this->pagination->create_links();?>   
</div></div>

Pagination Controller Code:-

File Name-[MemberController.php]

Read Also:  How to delete session Data in Laravel blade file?

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MemberController extends CI_Controller 
{
public function membersdatalist($start = 0)
{
$this->load->model('MemberModel');
$this->load->helper('country_helper');
$post_name = $this->input->get('search');
$model = $this->load->model('MemberModel');
$config['base_url'] = base_url().'index.php/memberController/membersdatalist/';
$totalRows = $this->MemberModel->searchMember($post_name);
$data['data'] = $this->MemberModel->searchMember($post_name, $start);		
$config['total_rows'] = count($totalRows);		
$config['per_page'] = 15; //data per page you want to display.
$this->load->library('pagination');
$this->pagination->initialize($config); 
$this->load->view('common/header_view');
$this->load->view('search_result_view',$data);
$this->load->view('common/footer_view');
} 
}
?>

Pagination model Code:-

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MemberModel extends CI_Model 
{
public function searchMember($post_name, $start=0) 
{
$this->db->select('*');
$this->db->from(MEMBERS);
$this->db->like('name', $post_name);
$this->db->or_like('uname', $post_name);
$this->db->or_like('email', $post_name);		 
$this->db->limit(10, $start);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}      
}
?>	

I hope you get an idea about dynamic pagination in codeigniter.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.