一个简单的分页类封装
本帖最后由 88646958@qq.com 于 2019-10-8 17:06 编辑先看代码
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class HS_Pagination extends CI_Pagination {
public $uri = '';
public function __construct($config = array())
{
parent::__construct($config);
$this->CI =& get_instance();
}
/**
* @param int $total 查询总条数
* @param int $uri_segmenturi的第几段包含的分页数 默认第四段。
*
*/
public function page($total,$uri_segment = 4)
{
for($x=1; $x<$uri_segment;$x++)
{
$this->uri .= $this->CI->uri->slash_segment($x, 'leading');
}
$config = array(
'base_url' => site_url($this->uri),
'total_rows' => $total,
'per_page' => 8,
'num_links' => 5,
'uri_segment' => $uri_segment,
'full_tag_open' => '<ul class="pagination justify-content-center">',//关闭标签
'full_tag_close' => '</ul>',
'first_link' => 'First',
'first_tag_open'=> '<li class="page-item pagination-first">',
'first_tag_close'=> '</li>',
'last_link' => 'Last',
'last_tag_open' => '<li class="page-item pagination-last">',
'last_tag_close'=> '</li>',
'next_link' => '>',
'next_tag_open'=> '<li class="page-item pagination-next">',
'next_tag_close'=>'</li>',
'prev_link' => '<',
'prev_tag_open'=> '<li class="page-item pagination-prev">',
'prev_tag_close'=>'</li>',
'cur_tag_open' => '<li class="page-item active"><a class="page-link" href="#">',
'cur_tag_close' => '</a></li>',
'num_tag_open' =>'<li class="page-item">',
'num_tag_close' =>'</li>',
);
$this->CI->pagination->initialize($config);
}
}
如何使用
将扩展的分页类文件放到你的应用的application/libaries目录
控制器
$this->load->library('pagination');
$this->pagination->page( $this->db->count('tabale'), 4);
默认uri的第四段包含分页,如果你的分段数是第三段开始写成
$this->pagination->page( $this->db->count('tabale'), 3);
一般我们的uri类似 这样 /控制器/方法/参数/参数/...有可能你的应用是第三段,或者第四段 也或者第五段
视图
<?php echo $this->pagination->create_links();?>
跟默认的分页类一样。
这样不用再每个查询的控制其写一堆分页的代码,也不用每次都指定 base_url 了
页:
[1]