php封装的page分页类完整实例 简单 通用
在网站中,不管是后台还是前端页面,如果list页内容太多,都会设置分页功能,分页显示,将从mysql数据库取出的数据有规律的一段段展示,利用sql语句中的limit,将它的起始数据跟页数绑定,根据页数去数据库取数据。
具体的分页功能怎么实现,原理呀,这里又不详细讲解了,不懂的可以看看相关的书籍或视频,这里就直接上代码了,一个封装好的通用的简洁的分页实例!
下面这个段代码是封装好的类,直接复制到page.class.php文件里,放到think文件夹下就行了,这是tp3.2代码样式,如果用其他框架请自行修改!
namespace Think; class Page { public $first_row; // 起始行数 public $list_rows; // 列表每页显示行数 public $parameter; // 分页跳转时要带的参数 public $total_rows; // 总行数 public $total_pages; // 分页总页面数 public $roll_page = 11; // 分页栏每页显示的页数 public $last_suffix = true; // 最后一页是否显示总页数 private $p = 'p'; //分页参数名 private $url = ''; //当前链接URL private $now_page = 1; // 分页显示定制 private $config = array( 'header' => '<span class="rows">共 %TOTAL_ROW% 条记录</span>', 'prev' => '<<', 'next' => '>>', 'first' => '1', 'last' => '%TOTAL_PAGE%', 'theme' => '%UP_PAGE% %FIRST% %LINK_PAGE% %END% %DOWN_PAGE% %HEADER%', ); /** * 架构函数 * @param array $total_rows 总的记录数 * @param array $list_rows 每页显示记录数 * @param array $parameter 分页跳转的参数 */ public function __construct($total_rows, $list_rows = 20, $parameter = array()) { C('VAR_PAGE') && $this->p = C('VAR_PAGE'); //设置分页参数名称 /* 基础设置 */ $this->total_rows = $total_rows; //设置总记录数 $this->list_rows = $list_rows; //设置每页显示行数 $this->parameter = empty($parameter) ? $_GET : $parameter; $this->now_page = empty($_GET[$this->p]) ? 1 : intval($_GET[$this->p]); $this->now_page = $this->now_page > 0 ? $this->now_page : 1; $this->first_row = $this->list_rows * ($this->now_page - 1); } /** * 定制分页链接设置 * @param string $name 设置名称 * @param string $value 设置值 */ public function setConfig($name, $value) { if (isset($this->config[$name])) { $this->config[$name] = $value; } } /** * 生成链接URL * @param integer $page 页码 * @return string */ private function url($page) { return str_replace(urlencode('[PAGE]'), $page, $this->url); } /** * 组装分页链接 * @return string */ public function show() { if (0 == $this->total_rows) return ''; /* 生成URL */ $this->parameter[$this->p] = '[PAGE]'; $this->url = U(ACTION_NAME, $this->parameter); /* 计算分页信息 */ $this->total_pages = ceil($this->total_rows / $this->list_rows); //总页数 if (!empty($this->total_pages) && $this->now_page > $this->total_pages) { $this->now_page = $this->total_pages; } /* 计算分页临时变量 */ $now_cool_page = $this->roll_page / 2; $now_cool_page_ceil = ceil($now_cool_page); $this->last_suffix && $this->config['last'] = $this->total_pages; //上一页 $up_row = $this->now_page - 1; $up_page = $up_row > 0 ? '<li><a href="' . $this->url($up_row) . '">' . $this->config['prev'] . '</a></li>' : '<li class="disabled"><span>' . $this->config['prev'] . '</span></li>'; //下一页 $down_row = $this->now_page + 1; $down_page = ($down_row <= $this->total_pages) ? '<li><a href="' . $this->url($down_row) . '">' . $this->config['next'] . '</a></li>' : '<li class="disabled"><span>' . $this->config['next'] . '</span></li>'; if ($this->total_rows <= $this->list_rows) { $up_page = $down_page = ''; } //第一页 $the_first = ''; if ($this->total_pages > $this->roll_page && ($this->now_page - $now_cool_page) >= 1) { $the_first = '<li><a class="first" href="' . $this->url(1) . '">' . $this->config['first'] . '...</a></li>'; } //最后一页 $the_end = ''; if ($this->total_pages > $this->roll_page && ($this->now_page + $now_cool_page) < $this->total_pages) { $the_end = '<li><a class="end" href="' . $this->url($this->total_pages) . '">...' . $this->config['last'] . '</a></li>'; } //数字连接 $link_page = ''; for ($i = 1; $i <= $this->roll_page; $i++) { if (($this->now_page - $now_cool_page) <= 0) { $page = $i; } elseif (($this->now_page + $now_cool_page - 1) >= $this->total_pages) { $page = $this->total_pages - $this->roll_page + $i; } else { $page = $this->now_page - $now_cool_page_ceil + $i; } if ($page > 0 && $page != $this->now_page) { if ($page <= $this->total_pages) { $link_page .= '<li><a href="' . $this->url($page) . '">' . $page . '</a></li>'; } else { break; } } else { if ($page > 0 && $this->total_pages != 1) { $link_page .= '<li class="active"><span>' . $page . '</span></li>'; } } } //替换分页内容 $page_str = str_replace(array('%HEADER%', '%NOW_PAGE%', '%UP_PAGE%', '%FIRST%', '%LINK_PAGE%', '%END%', '%DOWN_PAGE%', '%TOTAL_ROW%', '%TOTAL_PAGE%'), array($this->config['header'], $this->now_page, $up_page, $the_first, $link_page, $the_end, $down_page, $this->total_rows, $this->total_pages), $this->config['theme']); return '<ul class="pagination">' . $page_str . '</ul>'; } }
下面这段是css样式,需要什么样的改下就行了,我基本就是改改颜色,配合网站的主色;
/*分页*/ .pagination li {display: inline-block;margin:0 5px;padding: 5px;border: 1px solid #dcdcdc;min-width: 30px;text-align: center;} .pagination li.active {background: #0070c0;color: #fff;border: 1px solid #0070c0;} .pagination li a {display: block;text-align: center;}
控制器调用page分页的代码,page类放的正确的位置的话,tp3.2的可以直接用,其他框架的自行修改!
//合作伙伴 $page_size = C('PAGE_SIZE'); $count = $this->cooperate_model->where("is_show=1")->count('id'); $pager = new \Think\Page($count, $page_size, 3); $show = $pager->show(); $cooperate_list = $this->cooperate_model ->where("is_show=1") ->order('sort asc,id desc') ->limit($pager->first_row . ',' . $pager->list_rows) ->select(); $this->assign('cooperate_list', $cooperate_list); $this->assign('page', $show);
效果图:
作者头像
作者名称
作者性别
热门推荐
5 MVCC-乐观锁
评论列表