写了两个版本,还是后面好点,只计算关键数据,返回数组,html代码例外拼接
<?php /** * 分页类 * @author timo * @version 2016-12-11 08:15 */ class paginate{ //当前页码 private $page; //记录总条目 private $entry_total; //每页显示多少个记录 private $page_size; //纯数值li总数,即分页栏的长度(不包括首尾上下页) private $page_li_count; //总页数 private $page_count; private $result; private $active; //该url末尾必需带有?或者& private $url; /** * * @param unknown $url 跳转的链接地址 * @param unknown $entry_total 记录的总条目 * @param number $page_size 每页显示多少个记录 * @param number $page_li_count 分页栏显示多少个纯数值的li (不包括首尾上下页),即分页栏的长度 */ public function __construct($entry_total,$page_size = 5,$page_li_count = 1){ //初始化成员变量,参数还需要仔细筛选 $this->entry_total = (int)$entry_total; $this->page_size = (int)$page_size; $this->page_count = ceil($entry_total/$page_size); //分页栏的长度取用户提交的分页栏长度与页面总数中的较小值 $this->page_li_count = $page_li_count > $this->page_count ? $this->page_count : $page_li_count; } /** * 设置当前页及active状态 * @param unknown $page 合法参数为:first end 1234 */ public function setPage($page){ if ($page == ‘end‘) { $this->active = ‘end‘; $this->page = $this->getPagecount(); }elseif ($page>0 && $page<=$this->page_count) { $this->active = $page; $this->page = $page; }else{ //first或传递非法值的会被定到首页去 $this->active = ‘first‘; $this->page = 1; } } /** * * @param unknown $url //该url末尾必需带有?或者& */ public function setURL($url){ $this->url = $url; } //当前页码 public function getPage(){ return $this->page; } //共计多少页 public function getPagecount(){ return $this->page_count; } //共计多少条记录 public function getEntrycount(){ return $this->entry_total; } /** * 计算当前页的分页栏需要的信息 * @return 返回数组,包含拼接分页关键信息 */ public function create(){ //首页 $first_page= "first"; //计算上页页码,最小为1 $pre_page = ($this->page-1)<=1 ? 1 : $this->page-1; //计算下一页页码,最大为page_count $next_page = ($this->page+1)>=$this->page_count ? $this->page_count : $this->page+1; //尾页 $end_page= "end"; $nums_page = []; //这个判断与初始化变量中的判断重复了,但...安全点 if ($this->page_count >= $this->page_li_count) { //中间li为第几个,偶数取中偏前的一个 $mid_li = $this->page_li_count%2==0 ? $this->page_li_count/2 : (($this->page_li_count-1)/2)+1; //理想情况下,$mid_li后面li应有的个数 $right_li_num = $this->page_li_count - $mid_li; //理想情况下,$mid_li前面li应有的个数 $left_li_num = $mid_li-1; //计算最右边的页码 if ($this->page <= $left_li_num) { //若当前页左边li个数不足以让其居中,则最右端页码等于最右端li的编号 $right_li_page = $this->page_li_count; }else { $right_li_page = ($this->page_count-$this->page) >= $right_li_num ? $this->page+$right_li_num : $this->page_count; } for ($i = 0; $i < $this->page_li_count ; $i++) { //看着可能有点绕,纸上算算就知道了,已知最右边li的页码,li的总数,当前li的编号为$i+1,算当前li对应的页码 $n = $right_li_page-$this->page_li_count+$i+1; $nums_page[] = $n; } } //将这几个拼接起来 $this->result[‘first‘] = $first_page; $this->result[‘end‘] = $end_page; $this->result[‘pre‘] = $pre_page; $this->result[‘next‘] = $next_page; $this->result[‘nums‘] = $nums_page; $this->result[‘active‘] = $this->active; return $this->result; } public function getHTML(){ //待完善........ $url = isset($this->url) ? $this->url : ‘?‘; $first_class = $this->result[‘active‘] == ‘first‘ ? ‘active‘ : ‘‘; $end_class = $this->result[‘active‘] == ‘end‘ ? ‘active‘ : ‘‘; $pre_class = $this->result[‘active‘] == ‘pre‘ ? ‘active‘ : ‘‘; $next_class = $this->result[‘active‘] == ‘next‘ ? ‘active‘ : ‘‘; $first_page_html = <<<HTML <li class="$first_class"><a href="{$url}page={$this->result[‘first‘]}">首页</a></li> HTML; $pre_page_html = <<<HTML <li class="$pre_class"><a href="{$url}page={$this->result[‘pre‘]}">上一页</a></li> HTML; $next_page_html = <<<HTML <li class="$next_class"><a href="{$url}page={$this->result[‘next‘]}">下一页</a></li> HTML; $end_page_html = <<<HTML <li class="$end_class"><a href="{$url}page={$this->result[‘end‘]}">尾页</a></li> HTML; $lis_page_html = ‘‘; foreach ($this->result[‘nums‘] as $v){ $li_class = $this->result[‘active‘] == $v ? ‘active‘ : ‘‘; $lis_page_html .= <<<HTML <li class="$li_class"><a href="{$url}page=$v">$v</a></li> HTML; } $htmlcode = $first_page_html.$pre_page_html.$lis_page_html.$next_page_html.$end_page_html; return $htmlcode; } } /*********** * <?php require ‘paginate02.class.php‘; $a = new paginate(10,5,4); $a-> setPage( isset($_GET[‘page‘])?$_GET[‘page‘]:1); $a->create(); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>fenye</title> <link rel="stylesheet" href="css/bootstrap.css"> </head> <body> <ul class="pagination"> <?php echo $a->getHTML()?> </ul> </body> </html> * */
时间: 2024-10-12 12:31:51