Monday, September 16, 2013

CodeIgniter's Pagination with a Search Term

On this article I’ll try to explain how to use CodeIgniter’s pagination library with a search term in a way that that our links look like:

Controller\action\search_term\page

CodeIgniter’s pagination library makes it easy for us to show long data lists generating tha links for the corresponding pages automatically, But it lacks a way to include a search term on the paginated results.

The problem is that if we inlcude a search term with the pagination, there is no specific function to include this search teram on the links for the following pages. As an extra complication we will solve this problem including a subset of data, on which we will apply both the pagination and an optional search term. And getting the links like this:

Controller\accion\id\termino de búsqueda\pagina.

We have the following scenario:

 - A list of cities.
 - A list of people who belong to a city.
 - We want to list the people belonging to a determined city, and we want to paginate the results, but also we want to be able to search in this result set and have a paginated result, using the same view.

On the firts page, we have the cities listing:

 <a href="personas/ver/1"> Personas en Ciudad 1</a>
 <a href="personas/ver/2"> Personas en Ciudad 2</a>
 <a href="personas/ver/3"> Personas en Ciudad 3</a>

In person controller must have two actions "show" and "show_search", this second function serves as an intermediate function to pass the data from the search string in the case of a paged search and the first function in case we do not have a search string in which case it works as a normal paginated search.

The first function:

<?php
function show($id_city, $offset = 0){
       //her we verify if the we have to handle a serch term
       //or its comming from “show_search” with a seacr term
       if ( $this->session->userdata('var_search')){
             $search_term=$this->session->userdata('var_search');
             $this->session->unset_userdata('var_search');
       }else{
             $search_term ='';
       }

       $this->load->model('m_person');
      
       //Load the pagination library
       $this->load->library('pagination');

       //We defini the structure of the links in case we have a search term
if ($search_term ==''){
             $config['base_url'] = base_url().'/index.php/person/show/'.$id_city.'/';
             $config['uri_segment'] = '4';
       }else{
             $config['base_url'] = base_url().'/index.php/person/show_search/'.$id_city.'/'.$search_term.'/';
             $config['uri_segment'] = '4';
       }

//this function it’s up to you must count the results for the pagination
//library
       $config['total_rows'] = $this->m_person->number_of_persons($id_city,$seach_term);
       $config['per_page'] = 25;
       $config['num_links'] =5;
       $config['next_link'] = '>';
       $config['prev_link'] = '<';
       $config['first_link'] = '<<';
       $config['last_link'] = '>>';

       $this->pagination->initialize($config);
       $data["page_links"] = $this->pagination->create_links();

       //we obtain the list of people using the offset data and the number of records by page, Again this is up to you
$articulos=$this->m_personas->show_persons_paginated($id_city,$search_term,$offset,$config['per_page']);
       $data['persons']=$persons;
       $data['id_city']=$id_city;
       $this->load->vars($data);

       //cargamos nuestra vista
       $this->load->view('template');
}
?>

On this function we verified if we had a search term, and according to this we changed a Little bit the link we want the pagination to generate.


In case we don’t have a search term, the Base_url for the pagination is normal (and the pagination library will add the page number), In case of having a search term we use the “show_search” action which will be included in the links. This function is the following:

<?php

function show search($id_city,$search_term='',$offset = 0){
       if (empty($_POST)){
                   
       }else{
             try {
                    $search_term=$this->input->post(search_term);
             } catch (Exception $e) {
                    $search_term ='';
             }
       }
       $this->session->set_userdata('var_search_term', $search_term);
       redirect('/personas/show/'.$id_city.'/'.$offset);
}
?>

What this function does is, grab the search term and save it on a session variable, and then redirect to the normal function in which this session variable will be used. And so this little function solves the problem.

On the view in which we show the results and the pagination links, We must have some search form, like the following:

  <div align="center">
       <?php echo form_open('person/show_search/'.$id_city.'/'); ?>
       <?php echo form_input("search_term");?>
       <?php echo form_close();?>
 </div>


I hope this information can help you in the future, and if you have any questions fell free to ask.