Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using pagination with query string for a search form which has the method set to get in codeigniter

I'm banging my head on the keyboard trying to figure out a way to use query string with pagination every thing works fine until FIRST page link appears.

All other links have the query string appended to their end but the First page link misses the query string

Links for other pages:

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

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

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

The search form:

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

It has a textbox with the name set to q.

I stumbled upon a few answers/examples on stackoverflow and this is what I wrote:

The Pagination configuration file has

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

and others like num_tag_open etc.

The 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

        }
    }
}

S.O.S! Guys, please help me out

like image 728
Charanraj Golla Avatar asked May 21 '11 06:05

Charanraj Golla


1 Answers

I can't believe this, I spent hours searching the web for a solution ! It was always with me.I should have opened the pagination library and viewed its content before I posted this question.Just one line and problem solved.
I added the following line in the index method.
$config['first_url'] = '/index.php/search/index/?q='.$q;

like image 66
Charanraj Golla Avatar answered Oct 20 '22 11:10

Charanraj Golla