dqmchw0071 2014-10-19 10:03
浏览 49
已采纳

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

I have a codeigniter search form which includes a dropdown list (Car) and a checkbox array (Car types). I was using POST method to get values from database but post method conflicts with pagination so I decided to use GET method. But now my 'if' statement does not work and it returns me 'else' scenario (i.e. 'search_nok' page with a message "Please select your search options"). Could you please check my code and help me to find the mistake.

Here is my controller

   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);          
                 }              
           }
  • 写回答

1条回答 默认 最新

  • drjyvoi734793 2014-10-19 10:14
    关注

    That is because the validation class in CodeIgniter does not check for $_GET parameters and tries to validate the POST fields and finds no car or types.

    To complement this with a quick fix to validate the $_GET parameters you are sending (and since you have no POST) you could set the POST array to be the same as GET therefore passing the parameters to the validation class.

    $_POST = $_GET;
    

    this should be before the validation is run:

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

    UPDATE

    To keep the search parameters across pages

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测