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

一曲冷凌霜 提交于 2019-12-18 04:04:14

问题


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


回答1:


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;



来源:https://stackoverflow.com/questions/6080093/using-pagination-with-query-string-for-a-search-form-which-has-the-method-set-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!