CodeIgniter search and pagination, terms and uri segments
In this article I will show how I solved a problem that seems to be very attractive in the CodeIgniter community and nobody seems to want to show an efficient solution. I don’t know if this is one but what I know is that you have the option to build your view with or without search and you don’t need to sacrifice anything.
This solution was build having this in mind, a view method that can give me the search option having this form fields for the search what,where,from,to. Also I need to have the pagination option with the same functionality as it is now in the system. If you are in search for something similar try to follow this article and maybe this is your long searched solution.
Let’s begin. The view method:
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 |
function view(){ $this->load->library('pagination'); $total_seg = $this->uri->total_segments(); $offset = $this->uri->segment(3, ''); $uri_seg = 3; if($total_seg > 3){ $uri_str = substr($this->uri->uri_string(),1); $uri_arr = explode("/", $uri_str); $total_seg = count($uri_arr); $uri_seg = $total_seg+1; // $this->terms is defined as an empty array in the constructor $this->terms = $this->uri->uri_to_assoc(); if(($total_seg % 2) > 0){ $this->terms = array_slice($this->terms, 0 , (floor($total_seg/2)-1)); $offset = $this->uri->segment($total_seg, ''); $uri_seg = $total_seg; } } $total = $this->The_model->count_all($this->terms); $data['results'] = $this->The_model->get_results($this->terms, $this->results_per_page, $offset); $keys = $this->uri->assoc_to_uri($this->terms); $this->terms = array(); $config['base_url'] = base_url() . 'controller/view/' . $keys; $config['total_rows'] = $total; $config['per_page'] = $this->results_per_page; $config['uri_segment'] = $uri_seg; $config['num_links'] = 3; $this->pagination->initialize($config); $data['page_links'] = $this->pagination->create_links(); $this->load->view('the_view', $data); } |
The second step is to create our Model to extract the data from the database. I need to say about this that I’ve tried to simplify the method because I have a built in level based search so take it as it is and modify it as you like. I know that it can be made even more abstract but at this point in time and for this article is just enough. If you don’t have any idea yet read further and if at the end you’ll still be without any idea than this means that this article maybe is not what you are looking for adn I am sorry that I wasn’t able to help you.
First Model method, getting the results:
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 |
function get_results($terms = array(), $limit, $offset){ $this->db->select('table.*'); if(count($terms) > 0){ if(array_key_exists('what', $terms) && array_key_exists('where', $terms)){ $ww = " table." . $terms['where'] . " LIKE('%" . $terms['what'] . "%') "; $this->db->where($ww, NULL, FALSE); } if(array_key_exists('from', $terms)){ // the "from" field type is timestamp $from = " table.from >= " . strtotime($terms['from']) . " "; $this->db->where($from, NULL, FALSE); } if(array_key_exists('to', $terms)){ // the "to" field type is timestamp $to = " table.to <= " . strtotime($terms['to']) . " "; $this->db->where($to, NULL, FALSE); } } $this->db->order_by('table.id', 'DESC'); $this->db->limit($limit, $offset); $query = $this->db->get('table'); if($query->num_rows() > 0){ return $query->result(); } return FALSE; } |
The second method is the results counter. You’ll see the same piece of code as in the previous method and that’s why I said that this can be made even more abstract.
The counter:
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 |
function count_all($terms = array()){ $this->db->select('table.id'); if(count($terms) > 0){ if(array_key_exists('what', $terms) && array_key_exists('where', $terms)){ $ww = " table." . $terms['where'] . " LIKE('%" . $terms['what'] . "%') "; $this->db->where($ww, NULL, FALSE); } if(array_key_exists('from', $terms)){ $from = " table.from >= " . strtotime($terms['from']) . " "; $this->db->where($from, NULL, FALSE); } if(array_key_exists('to', $terms)){ $to = " table.to <= " . strtotime($terms['to']) . " "; $this->db->where($to, NULL, FALSE); } } $query = $this->db->get('table'); return $query->num_rows(); } |
Any suggestions are welcome. Make sure you leave a message.
Enjoy!

















First of all, nice website! Very interesting.
i have a question: whats the best way to implement the search and the ‘results per page’ config?
i do get all kind of errors(notice) about undefined vars. Maybe give it a update?
Regards Bart
Hi Bart,
I am preparing a new version for this article because I’ve changed my technique for quite a while now but I wanted to see what CodeIgniter 2.0 its all about and if my new article will fit into the scene. As you asked me for something new I will not wait any more and I will release in the next few days a new version of CodeIgniter search terms and pagination.
If you need an urgent answer please let me know and I will try to help you.
Regards,
Marius
Hi Marius. Have you managed to write that new version? I would like to have a look at it. thanks.
@yinyang78: Yes, I’ve changed the way I search using CI for a while now but I didn’t had very much time to add it to my blog. However, now that you’ve asked for it I will put it online. Probably I will use a different article.
So, later today you can come back for the code.
Regards,
Marius
How about the controller constructor & the view page, dear marius?