CodeIgniter Forums
Search with $term and pagination - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Search with $term and pagination (/showthread.php?tid=58277)

Pages: 1 2


Search with $term and pagination - El Forum - 05-29-2013

[eluser]the_unforgiven[/eluser]


Having trouble with a search feature on a site i have built with CI but having trouble with pagination and limiting to 20 items per page or something.

My controller i have:
Code:
public function program_search()
          {
      if ($this->input->post('term')){
        $this->front->set('title', 'Search Results');
        $this->front->set('res' ,  $this->wc_search->search($this->input->post('term')));

        $this->load->library('pagination');

        $config['base_url'] = base_url().'/site/program_search_results/';

        $this->db->where('JobRef',$this->input->post('term'));
        $this->db->or_where('WorkType',$this->input->post('term'));
        $this->db->or_where('Parish',$this->input->post('term'));
        $this->db->or_where('Location',$this->input->post('term'));

        $config['total_rows'] = $this->db->count_all_results('wc_program');
        $config['per_page'] = 10;
        $config['num_links'] = 20;
        $config['full_tag_open'] = '<div id="pagination">';
        $config['full_tag_close'] = '</div>';

        $this->pagination->initialize($config);    

        $this->front->buffer('content', 'site/program_search_results');
        $this->front->render();
    }
    else
    {
        $this->front->set('title', 'Program Search');
        $this->front->buffer('content', 'site/program_search');
        $this->front->render();
    }
}
Then in my model I have:
Code:
public function search($term)
        {
    $data = array();
    $this->default_select();
    $this->db->like('JobRef', $term);
    $this->db->or_like('Area', $term);
    $this->db->or_like('Parish', $term);
    $this->db->or_like('WorkType', $term);
    $this->db->or_like('Location', $term);

    $this->default_order_by();
    //$this->db->limit(15);
    $q = $this->db->get('wc_program');
        if ($q->num_rows() > 0)
        {
            foreach ($q->result_array() as $row)
            {
                $data[] = $row;
            }
        }
    $q->free_result();
    return $data;
}

So how do i get the pagination to work based on this code, i dont really want to alter the code already there has the search works fine, i just need it to show 15 records per page for whatever term is searched for.

Any help advise and code examples would be highly appreciated.



Search with $term and pagination - El Forum - 05-29-2013

[eluser]TheFuzzy0ne[/eluser]
Backup your current app (maybe throw it in a zip file or something), and then try implementing pagination. The user guide is pretty good, so there's not much point in us repeating it to you. If you get stuck, feel free to come back with a more specific problem.


Search with $term and pagination - El Forum - 05-30-2013

[eluser]the_unforgiven[/eluser]
Got the pagination working kinda of, but its not displaying the rest of the results it goes to the the uri of 30 for instance but still shows the first lot of data, what have i done wrong?


Search with $term and pagination - El Forum - 05-30-2013

[eluser]TheFuzzy0ne[/eluser]
You need to limit your results and also use an offset, so your model method should expect two more parameters, one for the page number or offset, and the other for the number of results. You will also need a method that will count the total results.

One thing you'll need to bare in mind, is how you keep the current search term. It's difficult to pass it through the URL due to the permitted_uri_chars. You may need to store it in the cookie, or to create a system that will assign a string consisting of characters that are permitted, which will be stored in the database along with the search parameters for that query.


Search with $term and pagination - El Forum - 05-30-2013

[eluser]the_unforgiven[/eluser]
I sused it out:

Model:
Code:
public function record_count() {
        return $this->db->count_all("wc_program");
    }

    public function fetch_all($limit, $start, $term) {

     $this->db->like('JobRef', $term);
  $this->db->or_like('Area', $term);
  $this->db->or_like('Parish', $term);
  $this->db->or_like('WorkType', $term);
  $this->db->or_like('Location', $term);
        $this->db->limit($limit, $start);
        $query = $this->db->get("wc_program");

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }

Controller:
Code:
public function program_search()
{
  $config = array();
        $config["base_url"] = base_url().'/site/program_search/';
        $config["total_rows"] = $this->wc_search->record_count();
        $config["per_page"] = 15;
        $config["uri_segment"] = 3;
        $config['num_links'] = 1;
        $config['full_tag_open'] = '<div class="pagination pagination-small"><ul>';
  $config['full_tag_close'] = '</ul></div>';
  $config['first_link'] = false;
  $config['last_link'] = false;
  $config['first_tag_open'] = '<li>';
  $config['first_tag_close'] = '</li>';
  $config['prev_link'] = '&laquo;';
  $config['prev_tag_open'] = '<li class="prev">';
  $config['prev_tag_close'] = '</li>';
  $config['next_link'] = '&raquo;';
  $config['next_tag_open'] = '<li>';
  $config['next_tag_close'] = '</li>';
  $config['last_tag_open'] = '<li>';
  $config['last_tag_close'] = '</li>';
  $config['cur_tag_open'] =  '<li class="active"><a href="#">';
  $config['cur_tag_close'] = '</a></li>';
  $config['num_tag_open'] = '<li class="page">';
  $config['num_tag_close'] = '</li>';

        
        $this->pagination->initialize($config);

        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data["results"] = $this->wc_search->fetch_all($config["per_page"], $page, $this->input->post('term'));
        $data["links"] = $this->pagination->create_links();

        $this->front->buffer('content', 'site/program_search_results', $data);
  $this->front->render();
}

Works a treat and with Bootstrap too for pagination, but thanks for your advise this prompted me to change it all and re-write it.


Search with $term and pagination - El Forum - 05-30-2013

[eluser]the_unforgiven[/eluser]
Although i have $this->input->post('term'); in the $data['results'] when i search for something knowing there's only like 5 results it still brings back all the results, leaving the pagination open to go throught the 100 pages, which i dont want, how can i get round this?


Search with $term and pagination - El Forum - 05-30-2013

[eluser]TheFuzzy0ne[/eluser]
The method you call to count all of the results needs to run an identical query to the one you use to get the results. The only difference being that there will be no limit of offset defined for the query. You can use query caching to cache part of your query so you can re-use it easily.

I like to do all of this in one method, so my model method will pass back an array that contains everything the pagination class requires, (such as total results, results per page and so on), as well as the results themselves. This makes it easier to utilise query caching, so you don't have to repeat your code over two methods.


Search with $term and pagination - El Forum - 05-30-2013

[eluser]the_unforgiven[/eluser]
So based on the code i posted how would i achieve this im not sure where or what i should put?


Search with $term and pagination - El Forum - 05-30-2013

[eluser]the_unforgiven[/eluser]
Can you help me? Smile


Search with $term and pagination - El Forum - 05-30-2013

[eluser]TheFuzzy0ne[/eluser]
Yes, I can help you. No, I can't do it for you.

If you have any questions, feel free to ask, and I'll do what I can to help.

In the meantime, this video might help: http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-search-results-without-query-strings-2/