CodeIgniter Pagination With Search Query

CodeIgniter Pagination With Search Query

I got inquires from readers about pagination in codeigniter with search or pass multiple query string in url.

So today i am going to explain you how to create simple query string or search pagination with the help of codeigniter exist pagination library.

Why we need pagination

You know we implement pagination while we need to retrieve a large data set from the database but we can’t display hole data together in single page so for that we dividing data into pagination segments like per page 10, 20, 30 50 result will be appear.

Here i am going to explain you step by step with the help of example how can we create codeigniter pagination with search.

Step 1. Create MySQL table “student”

In this step we will create a table name of table: “student”. Run this below sql commands in MySQL database to create a table “student” that stores student details.

CREATE TABLE student (
   id int(11) NOT NULL,
   name varchar(50) NOT NULL,
   rollno varchar(20) NOT NULL,
   class varchar(20) NOT NULL
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Step 2. Create view name of “student.php” inside (application/views) folder

Now we are going to create view file there we will place our html code. Create a view file “student” inside application/view folder and copy below code and past into your student view file.

<html>
<head>
<title>Search</title>
</head>
<body>
<form action="<?php echo base_url('search');?>" method="get">
<div class="col-md-8 pl-0 pr-0">
<input type="text" name="class" class="form-control textbox1" placeholder="Class name" required="" value="<?php echo $this->input->get('class');" />
</div>
<div class="col-md-4 pl-0 pr-0">
<button type="submit" class="btn btn-primary">Search Students</button>
</div>
</form>

<?php
if($res->num_rows()>0)
{
echo '<table class="table table-bordered"><tr><th>Name</th><th>Roll No.</th><th>Class</th></tr>';
foreach($res->result() as $search_result)
{
echo '<tr>'.$search_result->name.'<td></td><td>'.$search_result->rollno.'</td><td>'.$search_result->class.'</td></tr>';
}
echo '<table>';
}

echo $pagination;
?>
</body>
</html>

Step 3. Create Controller name of “Welcome.php” inside (application/controllers) folder

Here create a controller with name of “Welcome” inside this controller create a function name of the function is search. This controller search function will be handle search and pagination related work. To use functions and their functionality copy below code and past into your “welcome” controller.

class Welcome extends CI_Controller {
     function __construct() {
         parent::__construct();
         $this->load->library(array("pagination"));
         $this->load->model('M_welcome');
         $this->load->helper('url');
     }
     public function search()
     {
         $class = $this->input->get('class');
         $total_rows = $this->M_welcome->total_rows_by_class_name($class);
         $config = array();
         if ($this->input->get('class')) $config['suffix'] = '?' . http_build_query($_GET, '', "&");
         $config["base_url"] = site_url().'search';
         $config['first_url'] = $config['base_url'].'?'.http_build_query($_GET);
         $config["total_rows"] = $total_rows;
         $config["per_page"] = 12;
         $config['use_page_numbers'] = TRUE;
         $config['num_links'] = 4;
         $this->pagination->initialize($config);
         $data['pagination'] = $this->pagination->create_links();
         if($this->uri->segment(2)){
             $page = ($this->uri->segment(2)) ;
         }
         else{
             $page = 1;
         }
         $data['res'] = $this->M_welcome->get_student_by_class_name($class, $config["per_page"], $page);
         $this->load->view('include/header');
         $this->load->view('search', $data);
         $this->load->view('include/footer');
     }
 }

Step 4. Create model name of “M_welcome.php” inside (application/models) folder

Here we are create a model “M_welcome.php”. The model will be handle all database related work.

if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 /*
     Author: Mukesh Jakhar
     Description: Manage users model class
 */
 class M_welcome extends CI_Model{
     function __construct(){
         $this->load->database();
         parent::__construct();
     }
     public function total_rows_by_class_name($class)
     {
         $query  = $this->db->query("SELECT id FROM student WHERE class='$class'");
         return $query->num_rows();
     }
     public function get_student_by_class_name($class, $perpage, $pagenum)
     {
         if($pagenum>1)
         {
             $limit = $perpage;
             $start = $perpage * ($pagenum-1);
         }
         else
         {
             $limit = $perpage;
             $start = 0;
         }
         return $this->db->query("SELECT * FROM student WHERE class='$class' LIMIT $start, $limit");
     }
 }

Tags:
Pagination with Search Filter in CodeIgniter, CodeIgniter CRUD Operations with Search and Pagination, Ajax Pagination with Search and Filter in CodeIgniter, Pagination in Codeigniter with Step by Step Example, How to Paginate Search Results With CodeIgniter, CodeIgniter Pagination with Search Query Filter, codeigniter pagination with multiple search, codeigniter pagination with search and sort, ajax pagination with search and filter in codeigniter, codeigniter search with pagination solution, codeigniter pagination library download, codeigniter ajax pagination with search demo, dropdown search filter in codeigniter, codeigniter crud with pagination

About Author

My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.