Pagination in CodeIgniter With Multiple Search: In this blog post you will learn to make a pagination for number of records in PHP & CodeIgniter with Multiple Search options. As I am a developer I personally did not find any type of code for Multiple search in Internet. So I have shared the Source code with Database for Easy understanding.
Step-1
View page – welcome_message.php
Here I have given the page name welcome_message.php, But you can change the page name as per your requirement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | <!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Bootstrap CSS --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> <title>Hello, world!</title> </head> <body> <div class="container"> <div class="row"> <div class="col-md-12"> <h3 class="text-center">Pagination with Search filter</h3> <div class="card"> <div class="card-body"> <form action="<?= base_url() ?>" method="get"> <div class="row g-3 align-items-center"> <div class="col-auto"> <label for="search_text" class="col-form-label">Search</label> </div> <div class="col-auto"> <input type="text" placeholder="Search here" value="<?php if($this->input->get('search_text')){echo $this->input->get('search_text');}?>" id="search_text" name="search_text" class="form-control" aria-describedby=""> <br> <input type="text" placeholder="Mobile here" value="<?php if($this->input->get('search_mobile')){echo $this->input->get('search_mobile');}?>" id="search_mobile" name="search_mobile" class="form-control" aria-describedby=""> </div> <div class="col-auto"> <input type="submit" class="btn btn-primary" value="Search" > </div> </div> </form> <table class="table table-striped table-bordered" style="margin-top: 20px"> <thead> <tr> <th>SN</th> <th>Name</th> <th>Email</th> <th>Mobile</th> <th>Address</th> </tr> </thead> <tbody> <?php $i = 1; foreach ($users as $val) { ?> <tr> <td><?= $i++ ?></td> <td><?= $val['name'] ?></td> <td><?= $val['email'] ?></td> <td><?= $val['mobile'] ?></td> <td><?= $val['address'] ?></td> </tr> <?php } ?> </tbody> </table> </div> <?=$links?> </div> </div> </div> </div> <!-- Optional JavaScript; choose one of the two! --> <!-- Option 1: Bootstrap Bundle with Popper --> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script> <!-- Option 2: Separate Popper and Bootstrap JS --> <!-- <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script> --> </body> </html> |
Step-2
Controller- Welcome.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Welcome extends CI_Controller { /** * Index Page for this controller. * * Maps to the following URL * http://example.com/index.php/welcome * - or - * http://example.com/index.php/welcome/index * - or - * Since this controller is set as the default controller in * config/routes.php, it's displayed at http://example.com/ * * So any other public methods not prefixed with an underscore will * map to /index.php/welcome/<method_name> * @see https://codeigniter.com/userguide3/general/urls.html */ public function index() { $this->load->model('my_model'); //$this->data['users'] = $this->my_model->getusers(); $this->load->library('pagination'); $perPage=10; $config['base_url'] = base_url(); $page=0; if($this->input->get('page')) { $page = $this->input->get('page'); } $start_index=0; if($page != 0) { $start_index = $perPage * ($page - 1); } $total_rows = 0; if($this->input->get('search_text') != null || $this->input->get('search_mobile') != null) { $search_text = $this->input->get('search_text'); $search_mobile = $this->input->get('search_mobile'); $this->data['users'] = $this->my_model->getSearchUsers($perPage,$start_index,$search_text,$search_mobile,$is_count=0); //echo $this->db->last_query(); exit; $total_rows = $this->my_model->getSearchUsers(null,null,$search_text,$search_mobile,$is_count=1); } else { $this->data['users'] = $this->my_model->getSearchUsers($perPage,$start_index,null,null,$is_count=0); $total_rows = $this->my_model->getSearchUsers(null,null,null,null,$is_count=1); } $config['total_rows'] = $total_rows; $config['per_page']= $perPage; $config['enable_query_strings']= true; $config['use_page_numbers']= true; $config['page_query_string'] = true; $config['query_string_segment'] = 'page'; $config['reuse_query_string']= true; $config['full_tag_open']= '<ul class="pagination">'; $config['full_tag_close']= '</ul'> $config['first_link']= 'First'; $config['last_link']= 'Last'; $config['first_tag_open']= '<li class="page-item"><spann class="page-link">'; $config['first_tag_close'] = '</span></li>'; $config['prev_link']= '«'; $config['prev_tag_open'] = '<li class="page-item"><span class="page-link">'; $config['prev_tag_close'] = '</span></li>'; $config['next_link']= '»'; $config['next_tag_open'] = '<li class="page-item"><span class="page-link">'; $config['next_tag_close'] = '</span></li>'; $config['last_tag_open'] = '<li class="page-item"><span class="page-link">'; $config['last_tag_close'] = '</span></li>'; $config['cur_tag_open'] = '<li class="page-item active"><a class="page-link" href="#">'; $config['cur_tag_close'] = '</a></li>'; $config['num_tag_open'] = '<li class="page-item"><span class="page-link">'; $config['num_tag_close'] = '</span></li>'; $this->pagination->initialize($config); $this->data['page'] =$page; $this->data['links']= $this->pagination->create_links(); $this->load->view('welcome_message',$this->data); } } |
Step-3
Model- My_model.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | <?php class My_model extends CI_Model { /* function getusers() { $query = $this->db->get('users'); return $query->result_array(); } */ function getSearchUsers($perPage,$start_index,$search_text=null,$search_mobile=null,$is_count=0) { // echo $is_count; exit; $this->db->limit($perPage,$start_index); if($search_text != NULL) { $this->db->like('name',$search_text,'both'); $this->db->or_like('email',$search_text,'both'); // $this->db->or_like('mobile',$search_mobile,'both'); $this->db->or_like('address',$search_text,'both'); } if($search_mobile != NULL) { // $this->db->like('name',$search_text,'both'); // $this->db->or_like('email',$search_text,'both'); $this->db->or_like('mobile',$search_mobile,'both'); // $this->db->or_like('address',$search_text,'both'); } if($is_count==1) { $this->db->select('id'); $query= $this->db->get('users'); return $query->num_rows(); } else { $query = $this->db->get('users'); return $query->result_array(); } } } |