【问题标题】:Codeigniter paginate search results using get methodCodeigniter 使用 get 方法对搜索结果进行分页
【发布时间】:2014-12-14 11:25:33
【问题描述】:

我有一个 codeigniter 搜索表单,其中包括一个下拉列表(汽车)和一个复选框数组(汽车类型)。我使用 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);          
                 }              
           }

【问题讨论】:

  • 不,这是一个不同的问题
  • 同样的问题。您正在尝试验证通过$_GET 收到的表单。关于 SO 至少有 3 个相同的问题。
  • 不,我正在尝试使用 codeigniter 分页显示我的搜索结果。使用get还是post都没有关系,但是当我尝试验证结果时,它不适用于第二页。

标签: php codeigniter get pagination


【解决方案1】:

那是因为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()) {
    // ....
}

更新

保留search parameters across pages

【讨论】:

  • 感谢您的帮助。对此,我真的非常感激。我刚刚添加了 $_POST = $_GET;现在代码的工作方式与过去完全一样,即它正确显示第一页但是当我点击第二页时,搜索选项从 url 中消失,我得到“else”语句
  • 这意味着在您的视图中,您的链接不包括您当前所在的偏移量或页面,因此它可以在再次调用控制器时计算“下一个”页面。要么是这样,要么如果你的视图有这个问题,那么问题在于你的 $this->model_x->did_search 没有将正确的 $data 发送到视图
  • 第一次加载结果时 url 是:localhost/abc/cont/… 当尝试加载第二页时,url 是 localhost/abc/cont/search/5 我想知道为什么汽车和类型不包含在下一页的网址中
  • $_GET 的值提供给$_POST 破坏了它们的设计目的。但似乎,由于某种原因,这个解决方案在以前的场合已经被建议......这也很糟糕。
  • 这是 Codeigniter 的验证类验证 GET 参数的“已知”修复,只要您在没有要覆盖的 POST 的页面中,这应该没问题。
猜你喜欢
  • 2016-03-21
  • 1970-01-01
  • 1970-01-01
  • 2014-03-26
  • 2018-05-28
  • 2021-05-22
  • 1970-01-01
  • 2012-12-21
相关资源
最近更新 更多