CodeIgniter Forums
Pagination Problem with Search Results - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Pagination Problem with Search Results (/thread-25285.html)



Pagination Problem with Search Results - El Forum - 12-07-2009

[eluser]cinoob[/eluser]
I am trying to create pagination for when a user searches for a term through an input field and options from a drop down list.

In order to preserve what the user has requested when going from one page to another I decided to append the search terms to the url in this format:

search/index/searchTerm/searchType/offset

The form posts to a function which redirects to the correct url as follows:

Code:
function alias_search() {
        redirect('/search/index/'.$this->input->post('search').'/'.$this->input->post('searchType').'/0/', 'location', 301);
    }

Then I have my index function doing the pagination and retrieving of records:

Code:
function index() {
        
        $data['title'] = 'search results';
        
                $this->load->model('ModelName');
                $this->load->library('pagination');
        
        
        //pagination config
        $config['base_url'] = base_url().'index.php/search/index/'.$this->uri->segment(3).'/'.$this->uri->segment(4);
        $config['per_page'] = 2;
        $config['num_links'] = 10;
        $config['full_tag_open'] = '<div class="pagination">';
        $config['full_tag_close'] = '</div>';
        
        
        $data['searchType'] = $this->uri->segment(4);
        $searchType = $data['searchType'];
        
        
        $config['total_rows'] = $this->ModelName->search_events_rows();
        $this->pagination->initialize($config);

        $data['queryevents'] = $this->ModelName->search_events();
        $data['main_content'] = 'search_view';
        $this->load->view('includes/template', $data);
}


The $this->ModelName->search_events_rows() function returns the number of rows from the database and $this->ModelName->search_events() gets all the records.

The search_events() function:

Code:
function search_events()
    {    
        $todays_date = date("Y-m-d H:i:s");
        $config['per_page'] = 2;

        $this->db->select('*')->from('events')->like('title', $this->uri->segment(3))->limit($config['per_page'], $this->uri->segment(5));
        
        $query = $this->db->get();
        return $query;
        
    }

The pagination links are also displaying fine in my view using
Code:
&lt;?php echo $this->pagination->create_links(); ?&gt;

Clicking on the second page link goes to the correct url but still shows the first page of results and the first link in the pagination is still showing as the current page.

Any ideas as to why this happening?


Pagination Problem with Search Results - El Forum - 12-07-2009

[eluser]cinoob[/eluser]
Think I've managed to fix this using

Code:
$config['uri_segment'] = 5;