Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter Search Results Pagination

Well I've searched and searched all around but I still can't find a solution to my problem. I'm still new to php and codeigniter so maybe I missed the answer already but anyways, here's what I'm trying to do.

This is my Controller (c_index.php) - calls a search function and performs pagination on the resulting array.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class C_index extends CI_Controller {


public function __construct()
{
    parent::__construct();
    $this->load->model('m_search');
    $this->load->library("pagination");
    $this->load->library("table");
}
/** Index Page for this controller */
public function index()
{
    if($this->session->userdata('logged_in')){
        $this->load->view('v_user');    
    }   
    else{
        $this->load->view('index'); 
    }
}

public function search() 
{
            // start of search
    $search_term = $this->input->post('word');
    $books = $this->m_search->search_books($search_term);

            // start of pagination
    $config['base_url'] = "http://localhost/test/index.php/c_index/search";
    $config['per_page'] = 5;
    $config['num_links'] = 7;
    $config['total_rows'] = count($books);

    echo $config['total_rows'];
    $this->pagination->initialize($config);
    $data['query'] = array_slice($books,$this->uri->segment(3),$config['per_page']);
    $this->load->view("index",$data);

}

}

Here's my view (index.php) - basically just displays the pagination result

<h3> Search Results </h3>   
    <!-- table -->              
    <table class="table table-condensed table-hover table-striped" id="result_table">
        <tbody>
        <?php
        if(isset ($query)){
            if(count($query)!=0){
                foreach($query as $item){
                echo "<tr><td>". $item['title'] ." by " .$item['author'] ."<br/></td></tr>";
                }
                echo $this->pagination->create_links();
            }
            else
            {
                echo "No results found for keyword ' ". $this->input->post('word')." ' .";
            }
        }
        else
        {
            echo "Start a search by typing on the Search Bar";
        }
        ?>              
        </tbody>
    </table>

My model (m_search.php) - basically searches the database and returns an array of results.

<?php

class M_search extends CI_Model{

function search_books($search_term='default')
{

    $filter = $this->input->post('filter');
    //echo $filter;

    if($filter == 'title')
    {
        //echo 'title';
        $this->db->select('*');
        $this->db->from('book');
        $this->db->like('title',$search_term);
        // Execute the query.
        $query = $this->db->get();
        return $query->result_array();

    }else if($filter == 'author')
    {
        //echo 'author';
        $this->db->select('*');
        $this->db->from('book');
        $this->db->like('author',$search_term);
        // Execute the query.
        $query = $this->db->get();
        return $query->result_array();

    }else if($filter == 'type')
    {
        //echo 'type';
        $this->db->select('*');
        $this->db->from('book');
        $this->db->like('book_type',$search_term);
        // Execute the query.
        $query = $this->db->get();
        return $query->result_array();

    }else if($filter == 'status')
    {
        //echo 'status';
        $this->db->select('*');
        $this->db->from('book');
        $this->db->like('book_status',$search_term);
        // Execute the query.
        $query = $this->db->get();
        return $query->result_array();
    }else
    {
        //echo 'all';
        $this->db->select('*');
        $this->db->from('book');
        $this->db->like('book_status',$search_term);
        $this->db->or_like('book_type',$search_term);
        $this->db->or_like('author',$search_term);
        $this->db->or_like('title',$search_term);
        // Execute the query.
        $query = $this->db->get();
        return $query->result_array();
    }

}

}

Now my problem is keeping the results for the pagination.

The first page is fine but whenever I click on a page link, the results on the table show the whole database and is not limited to my search results.

I've read somewhere that I need to use sessions to keep my search_term so that it works when I switch pages but I don't know where to put it. Any advice or suggestions would be greatly appreciated. Thanks.

like image 266
user3348358 Avatar asked Oct 01 '22 12:10

user3348358


1 Answers

There are several of different ways to handle search and pagination depending on your needs. Based on your existing code, this is what I would do.

Change

$search_term = $this->input->post('word');

to

$search_term = ''; // default when no term in session or POST
if ($this->input->post('word'))
{
    // use the term from POST and set it to session
    $search_term = $this->input->post('word');
    $this->session->set_userdata('search_term', $search_term);
}
elseif ($this->session->userdata('search_term'))
{
    // if term is not in POST use existing term from session
    $search_term = $this->session->userdata('search_term');
}
like image 159
Samutz Avatar answered Oct 13 '22 10:10

Samutz