对搜索表单使用带有查询字符串的分页,该表单的方法设置为在 codeigniter 中获取

2022-01-04 00:00:00 get php codeigniter pagination

我正在敲击键盘,试图找出一种使用带有分页的查询字符串的方法,一切正常,直到 FIRST 页面链接出现.

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.

所有其他链接的末尾都附加了查询字符串,但 First 页面链接 缺少查询字符串

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

其他页面的链接:

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

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

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

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 的文本框.

It has a textbox with the name set to q.

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

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

分页配置文件有

$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

        }
    }
}

求救!伙计们,请帮帮我

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

推荐答案

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

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;

相关文章