【问题标题】:Filter, sort and paginate in Codeigniter在 Codeigniter 中过滤、排序和分页
【发布时间】:2010-03-11 21:04:04
【问题描述】:

我最近开始使用 CodeIgniter,因为我正在寻找一个非常轻量级的框架,它似乎是首选。

我是整个 MVC 的新手,很享受它,但我被一些看似非常简单的东西所困扰。

我正在编写一个 CMS,需要一种方法来过滤、排序和分页结果。我习惯于使用查询字符串来做这件事,所以我会有一些类似的东西:

articles.php?order=title&sort=desc&filter=articletitle&page=5

我不知道如何在 CI 中执行此操作,所以我只是在配置中打开了 EnableQueryStrings,它工作正常,但我感觉它可能不是最优雅的解决方案。

我想我可以有

index.php/articles/index/order/title/sort/desc/filter/articletitle/page/5

但对我来说,这似乎很不灵活,例如,如果我不需要排序,我将如何确保我正在查看正确的 uri 段?

有什么想法吗?

【问题讨论】:

    标签: php codeigniter content-management-system query-string


    【解决方案1】:

    您是否尝试过实现_remap 函数?它将所有查询重定向到控制器到该函数,允许您实现任意数量(或尽可能少)的那些。

    然后你可以这样做:

    class Articles extends Controller
    {
        // Note: No need for a "order" or even an "index" function!
        public function _remap()
        {
            $assoc = $this->uri->uri_to_assoc( 4 );
            /* $assoc is now 
               array( "order"=>"title",
                      "sort"=>"desc",
                      "filter"=>"articletitle", 
                      "page"=>5); */
        }
    }
    

    【讨论】:

      【解决方案2】:

      我已经多次遇到这个问题,最后决定尝试正确解决它。

      我的解决方案涉及组合路径/查询字符串方法(它需要a solution like the one Stephen linked to)。

      网址格式如下:

      http://www.myapp.dev/controller/index/10?order_by=id+asc&status=open

      这些可选的 $_GET 参数然后可以用作查询条件,并且您可以根据需要使用任意多个,而不会搞砸 CI 的分页偏移量。

      默认情况下,CodeIgniter 的分页库不支持将偏移量放在 URI 的末尾之前。让 CI 支持这一点的诀窍是像这样扩展分页库:http://pastie.org/1393513

      然后,在您的控制器中,您可以像这样初始化分页:

      $config['url_format'] = site_url('controller/index/{offset}?'.http_build_query($params));
      $config['total_rows'] = $this->model->count_rows();
      $config['per_page'] = 5;
      $this->pagination->initialize($config);
      

      请注意,不需要 uri_segment,因为自定义 Pagination::initialize 方法会根据 {offset}url_format 字符串中的位置检测它。

      使用$this->pagination->create_links() 构建的链接将在适当的位置插入偏移量,并保留尾随查询字符串。

      【讨论】:

        【解决方案3】:

        我不喜欢 CodeIgniter 破坏查询字符串的事实。查询字符串非常适合可选参数。尝试将可选参数放在 URI 段中,事情开始变得很奇怪。

        这样的 URL 似乎有点 hack :

        index.php/articles/index/order/title/sort/desc/filter/articletitle/page/5
        

        出于这个原因,我将 CodeIgniter 配置为混合使用 URI 段和查询字符串。 This answer shows you how you can achieve this.

        【讨论】:

          【解决方案4】:

          默认情况下不能使用查询字符串有点尴尬。通过在表单帖子中发送排序首选项,然后将该首选项存储在用户的会话中,我已经在我当前的项目中的几个方面克服了这个问题。

          【讨论】:

            【解决方案5】:

            你总是可以这样做:

            www.yourdomain.com/articles/order-title/sort-desc/filter-articletitle/page-5
            

            通过使用 .htaccess 删除 index.php。然后在每个 URI 段的“-”上展开。

            $order = $this->uri->segment(2, 0);
            $sort = $this->uri->segment(3, 0);
            $filter = $this->uri->segment(4,0);
            $page = $this->uri->segment(5, 0;
            
            if(!empty($order)){
                $order = explode('-', $order);
            } else {
                $order = 'defaultorder';
            }
            
            /** And so on for the rest of the URI segments **/
            

            或者甚至从等式中删除整个爆炸,然后做:

            www.yourdomain.com/articles/title/desc/articletitle/5
            
            $order = $this->uri->segment(2, 0);
            $sort = $this->uri->segment(3, 0);
            $filter = $this->uri->segment(4,0);
            $page = $this->uri->segment(5, 0;
            

            这就是大多数使用 Code Igniter 的主要企业进行排序和分页的方式。

            【讨论】:

              【解决方案6】:

              我个人是 Lou 建议的忠实粉丝或路径/查询字符串组合,但我认为我有一个比他更简单的方法。

              我的解决方案将每页参数与您的过滤参数一起放在查询字符串中,例如

              http://mysite.com/admin/contacts?status=open&per_page=20
              

              感谢分页类的 page_query_string 选项。这里是:

              $query_string = some_function_to_get_your_filter_query_string_params();
              
              $config['base_url'] = site_url("admin/contacts?". $query_string);
              $config['total_rows'] = $total_rows;
              $config['per_page'] = $per_page;
              $config['page_query_string'] = TRUE;
              $this->pagination->initialize($config);
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2018-01-13
                • 1970-01-01
                • 2013-05-08
                • 2013-02-19
                • 1970-01-01
                • 2015-08-19
                • 1970-01-01
                • 2018-06-22
                相关资源
                最近更新 更多