Using pagination with a query string for a search form that has a method set to receive in codeigniter

I'm banging my head on the keyboard, trying to figure out how to use the pagination query string, everything works fine until the FIRST link appears.

In all other links, the query string is added to the end, and the link is FIRST page misses the query string

Links for other pages:

 http://localhost/index.php/search/index/9?q=some_Data_From_Form 

The link to the FIRST page shows the link that I set in the $config['base_url'] variable:

 http://localhost/index.php/search/index/ 

Search Form:

 $attributes=array('id'=>'search','class'=>'clearfix','method'=>'get'); echo form_open(base_url().'index.php/search/index',$attributes); 

It has a text box with the name set to q .

I came across some answers / examples in stackoverflow, and here is what I wrote:

Pagination configuration file has

 $config['per_page'] = '1'; $config['uri_segment'] = '3'; 

and others like num_tag_open etc.

Controller Class:

 class Search extends CI_Controller { public function Search(){ parent::__construct(); $this->load->helper('url'); $this->load->helper('form'); $this->load->library('input'); $this->load->model('blog_model'); $this->load->library('pagination'); $this->config->load('pagination'); //other pagination related config variables } public function index($page=0){ $q = trim($this->input->get('q')); if(strlen($q)>0){ //validate input and show data $config['enable_query_strings']=TRUE; $getData = array('q'=>$q); $config['base_url'] = 'http://localhost/index.php/search/index/'; $config['suffix'] = '?'.http_build_query($getData,'',"&"); $data['rows'] = $this->blog_model->getBySearch($q,$this->config->item('per_page'),$page); if(empty($data['rows'])){ //no results found }else{ //match found $config['total_rows'] = $this->blog_model->getBySearchCount($q); $this->pagination->initialize($config); $link->linkBar = $this->pagination->create_links(); $this->load->view('myview',array($data,$link)); } }else if(strlen($q)==0){ //warn user for the missing query and show a searchbox } } } 

SOS! Guys, please help me.

+8
php get codeigniter pagination
source share
1 answer

I can’t believe it, I spent hours searching the Internet for a solution! It has always been with me. I needed to open the pagination library and view its contents before I posted this question. One single issue and issue resolved.
I added the following line in the index method. $config['first_url'] = '/index.php/search/index/?q='.$q;

+10
source share

All Articles