Pagination in CodeIgniter With Search Query

Pagination in CodeIgniter 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

Author: Mukesh Jakhar

This is Mukesh Jakhar. I’m a professional web developer with 3+ years experience. I am always ready for freelance work and i am writing blogs in my free time. I love to learn new technologies and share with others.
I founded PHPKIDA in September 2015. The focus of this website to provide web development tutorials of PHP, WordPress, CodeIgniter, Jquery, MySQL, HMTL, CSS etc and sharing solution to problems which i already solved.

1 thought on “Pagination in CodeIgniter With Search Query”

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts: You may like

Online Directory Software – Local Business Directories Application

Codeigniter pagination with search query string

Minify HTML in CodeIgniter using Hooks

Redirect http to https using htaccess

How to execute mysql query inside codeigniter helper

Upload and Resize Image and Create Thumbnail Codeigniter

Rename file while uploading in codeigniter

Pagination In CodeIgniter With Example

Upload file with database codeigniter

Simple codeigniter login system

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Free online tool to download Instagram videos, photos and save Instagram videos, photos to your device. It's free & easy to use.

Download Now

CURRENCY CONVERTER

Our real time currency converter calculator will convert your money based on current values from around the world. It's free & easy to use.

Convert Now

ROBOST.TXT GEN.

Create robots txt file that help search engines to crawling and indexing your site with help of our free online Robots.txt Generator tool.

Create Now

HTML MINIFIER

Free online tool to minify or compress html code and reduce the size of html code in few clicks. It's free & easy to use.

Minify Now

CSS MINIFIER

Free online tool to minify or compress css code and reduce the size of css code in few clicks. It's free & easy to use.

Minify Now

JAVASCRIPT MINIFIER

Free online tool to minify or compress javascript code and reduce the size of javascript code in few clicks. It's free & easy to use.

Minify Now

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.