Codeigniter使用get方法对搜索结果进行分页


Codeigniter paginate search results using get method

我有一个代码点火器搜索表单,其中包括一个下拉列表(汽车)和一个复选框数组(汽车类型)。我使用POST方法从数据库中获取值,但POST方法与分页冲突,所以我决定使用get方法。但现在我的"if"语句不起作用,它返回给我"else"场景(即"search_nok"页面,并显示消息"请选择您的搜索选项")。你能检查一下我的代码并帮我找出错误吗。

这是我的控制器

   public function search($offset = 0) {
                $limit = 5;
                $this->load->library('form_validation');
                $this->load->model('model_x');
                $this->form_validation->set_rules('car', 'Car','required');
                $this->form_validation->set_rules('types', 'Car Type','required');
               if($this->form_validation->run()) {
        $car= $this->input->get('car');
        $types = $this->input->get('types'); 
        $this->load->library('pagination');
        $config['base_url'] = 'http://localhost/abc/cont/search/'; 
        // 'http://localhost/abc' is my base url
        $config['total_rows'] = 14;
        $config['per_page'] = 5; 
        $data['pagination'] = $this->pagination->initialize($config);
                if ($this->model_x->did_search($car, $types, $limit, $offset)){
                $data["results"] = $this->model_x->did_search($car, $types, $limit, $offset); 
                $this->load->view("search_ok",$data);           
                }          
                }
                else
                {
                $data['message'] = 'Please select your options.';   
                $this->load->view("search_nok",$data);          
                 }              
           }

这是因为CodeIgniter中的验证类不检查$_GET参数,并尝试验证POST字段,但未找到cartypes

为了补充这一点,可以通过快速修复来验证您正在发送的$_GET参数(并且由于您没有POST),您可以将POST数组设置为与GET相同,从而将参数传递给验证类。

$_POST = $_GET;

这应该在验证运行之前:

$_POST = $_GET;
$this->form_validation->set_rules('car', 'Car','required');
$this->form_validation->set_rules('types', 'Car Type','required');
if($this->form_validation->run()) {
    // ....
}

更新

保持页面间的搜索参数