对搜索表单使用分页和查询字符串,该表单将方法设置为获取代码点火器


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

我正在敲打键盘,试图找出一种将查询字符串与分页一起使用的方法,直到出现FIRST页面链接之前,一切都可以正常工作。

所有其他链接的末尾都附加了查询字符串,但First页面链接misses the query string

其他页面的链接:

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

第一页链接显示我在$config['base_url']中设置的链接变量:

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

搜索表单:

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

它有一个文本框,名称设置为 q .

偶然发现了一些关于stackoverflow的答案/示例,这就是我写的:

分页配置文件具有

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

和其他喜欢num_tag_open等。

控制器类:

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
        }
    }
}

嗖!伙计们,请帮帮我

我简直不敢相信,我花了几个小时在网上搜索解决方案!它总是和我在一起。在发布此问题之前,我应该打开分页库并查看其内容。只需一行,问题就解决了。
我在索引方法中添加了以下行。
$config['first_url'] = '/index.php/search/index/?q='.$q;