在Codeigniter中过滤,排序和分页 [英] Filter, sort and paginate in Codeigniter
问题描述
Ive最近开始使用CodeIgniter,因为我在寻找一个非常轻量级的框架,它似乎是一个顶级的选择。
Ive recently started using CodeIgniter as I was searching for a very lightweight framework and it seemed to come up as a top choice.
我是新的整个MVC事情,享受它,但被困住似乎很简单的东西。
I am new to the whole MVC thing, enjoying it but am stuck with something which seems very simple.
我编写一个CMS,需要一种方法来过滤,排序和分页的结果。我习惯了使用querystrings,所以我会有一些类似于:
I am coding a CMS and need a way to filter, sort and paginate the results. I am used to doing it with querystrings, so I would have something along the lines of:
articles.php?order=title&sort=desc&filter=articletitle&page=5
我不知道我会如何在CI中这样做,所以我只是打开EnableQueryStrings在配置和它的工作正常,但我得到的感觉它可能不是最优雅的解决方案。
I have no idea how I would go about doing this in CI so I just turned on the EnableQueryStrings in the config and it works fine, but i am getting the feeling its probably not the most elegant solution.
我可以有
index.php/articles/index/order/title/sort/desc/filter/articletitle/page/5
但对我来说这看起来很不灵活,如果我不需要排序,确保我正在查看正确的uri段。
but to me this seems very inflexible, what if for example i dont need to sort, how would i make sure i am looking at the correct uri segment?
任何想法?
推荐答案
您是否尝试过实现 _remap
函数?它会将所有查询重定向到控制器到该函数,允许你实现尽可能多的(或尽可能少的)。
Have you tried implementing the _remap
function? It will redirect all queries to the controller to that function allowing you to implement as many (or as few) of those as you like.
你可以这样做:
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); */
}
}
这篇关于在Codeigniter中过滤,排序和分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!