Codeigniter与过滤器和订单的好分页

时间:2013-10-16 15:03:45

标签: php codeigniter pagination

我在多个Codeigniter项目中工作,并找到这种方式对搜索进行分页而不会丢失过滤器和订单。

您可以通过以下方式为分页链接定义后缀:

$config['suffix'] = YOUR_SUFFIX

示例:

网址:http://www.test_store.com/products/index/search/foo/order/low_price

// Parsing the URI
$uri = $this->uri->uri_to_assoc(3);

$page     = !empty($uri['page'])   ? $uri['page']   : 1;
$order    = !empty($uri['order'])  ? $uri['order']  : false;
$search   = !empty($uri['search']) ? rawurldecode($uri['search']) : false;

// Search paginated
$this->Product->limit  = $per_page;
$this->Product->offset = ($page - 1) * $per_page;
$this->Product->order  = $order;
$this->Product->search = $search;

$products = $this->Product->get_all();

// Pagination config
$config['base_url']         = site_url('products/index/page');
$config['total_rows']       = $this->Product->count_all();
$config['uri_segment']      = 4;
$config['num_links']        = 5;
$config['use_page_numbers'] = TRUE;
$config['per_page']         = $per_page; 

// Add a suffix to pagination links (The key 'suffix' isn't documented apparently)
unset($uri['page']); // Don't needed
$config['suffix'] = !empty($uri) ? '/'. $this->uri->assoc_to_uri($uri) : ''; 

$this->pagination->initialize($config); 

$pagination = $this->pagination->create_links();

接受更正!

PD:我是阿根廷人,我的英语技能有点差。

0 个答案:

没有答案