Codeigniter Pagination Class Library


Codeigniter Pagination Class Library -We can load pagination class library like this $this->load->library(‘pagination’);. This library provides various functions that are used to create pagination class. It is 100% customizable. Here in this tutorial, we are going to explain how to use pagination class library.


Codeigniter pagination class library

Let us understand how pagination class library works in codeigniter with examples.

Load migration class library

First load pagination class library to use its functions, this library can be loaded simply as below-

How to load pagination class library:

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

Functions:-

There are following functions available in pagination class library. Now we will explain one by one.

  • 1. Hiding the pages.
  • 2. Adding attribute to anchors.
  • 1. Disabling the “rel” attribute.

1. Hiding the pages.

Here is simple demo of hiding the pages.

This function is used to hide a 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://example.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();
}
}
?>

Output will be like this:-

Codeigniter Pagination Class Library

2. Adding attribute to anchors.

Here is simple demo of adding attribute to anchors.

This function is used to add the attributes in pagination.

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

3. Disabling the “rel” attribute.

Here is simple demo of disabling the “rel” attribute.

This function is used to generate dynamic relation attributes.

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

Class reference:-

There are following references available in pagination class library. Now we will explain.

1. Initialize.

This reference is used to initialize to pagination class.

initialize([$params = array()])
  • Parameters :
  • $params (array) : Configuration parameters
  • Returns : CI_Pagination instance
  • Returns type : CI_Pagination

2. Create links.

This reference is used to return a pagination bar.

create_links()
  • Parameters :
  • Returns : HTML-formatted pagination
  • Returns type : string
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://example.com/index.php/test/page/';
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
}
}
?>

Output will be like this:-

Codeigniter Pagination Class Library

More Example About Pagination

Let us see in details about pagination example.

Pagination View Code:-

File Name-[search_result_view.php]

<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 sucessfull</b></p>";
}
?> 
<div><p id="error"> </p></div>
<form method="post" action="<?php echo base_url(); ?>index.php/UserController/showlist">
<div class="form-group">
<input type="text" name= "search" class="form-control" placeholder="Name, UserName And Email">
<button class="btn btn-info">Search Now</button>
<a href="<?php echo base_url('index.php/UserController/register/');?>" class="btn btn-info");">Add Users</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>User 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/UserController/editUser/'.$row['id']);?>" class="btn btn-primary" onclick="return confirm('Are you sure you want to Update this item?');">Edit</a></td>
<td><a href="<?php echo base_url('index.php/UserController/deleteAction/'.$row['id']);?>" class="btn btn-warning" onclick="return confirm('Are you sure you want to Delete this item?');">Delete</a></td></tr>
<?php
}
}
 ?>
</table>
<?php echo $this->pagination->create_links();?>   
</div></div>

Pagination Controller Code:-

File Name-[UserController.php]

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class UserController extends CI_Controller 
{
public function showlist($start = 0)
{
$this->load->model('UserModel');
$this->load->helper('country_helper');
$title = $this->input->get('search');
$model = $this->load->model('UserModel');
$config['base_url'] = base_url().'index.php/userController/showlist/';
$totalRows = $this->UserModel->searchUser($title);
$data['data'] = $this->UserModel->searchUser($title, $start);		
$config['total_rows'] = count($totalRows);		
$config['per_page'] = 5; //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:-

File Name-[UserModel.php]

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

Output will be look like this:-


Advertisements

Add Comment

📖 Read More