分页类
<?php //分页类 class Page{ private $total; //总记录数 private $nums; //每页显示的条数 private $pages; //总页数 private $cpage; //当前页 private $url; //当前url public function __construct($total,$nums){ $this->total = $total; $this->nums = $nums; $this->pages = $this->getPages(); $this->url = $this->setUrl(); //获取当前页 $this->cpage = !empty($_GET['page']) ? intval($_GET['page']):1; $this->cpage = $this->cpage>0 ?$this->cpage:1; $this->cpage = $this->cpage>$this->pages?$this->pages:$this->cpage; } private function setUrl(){ $url = $_SERVER['REQUEST_URI']; if(strstr($url, "?")){ $arr = parse_url($url); if(isset($arr['query'])){ parse_str($arr['query'],$output); } //删除url中重复的page=n的字符串 unset($output['page']); $url = $arr['path']."?".http_build_query($output); }else{ $url .="?"; } return $url; } //获取页数 private function getPages(){ return ceil($this->total/$this->nums);// 向上取整法 } //首页 private function first(){ //如果当前页是第一页 则不显示这些 if($this->cpage>1){ $pre = $this->cpage-1; return '<a href="'.$this->url.'&page=1">首页</a> <a href="'.$this->url.'&page='.$pre.'">上一页</a>'; }else{ return ''; } } //列表 private function flist(){ $list = ""; $num = 4; //当前页之前的设置 for($i=$num;$i >=1;$i--){ $page = $this->cpage-$i; if($page >1){ $list .= ' <a href="'.$this->url.'&page='.$page.'">'.$page.'</a> '; } } //当前页的设置 if($this->pages>1){ $list .=' '.$this->cpage.' '; } //当前页之后的设置 for($i=1;$i <=$num;$i++){ $page = $this->cpage+$i; if($page <= $this->pages){ $list .= ' <a href="'.$this->url.'&page='.$page.'">'.$page.'</a> '; }else{ break; } } return $list; } //末页 private function last(){ if($this->cpage < $this->pages){ $next = $this->cpage+1; return '<a href="'.$this->url.'&page='.$next.'">下一页</a> <a href="'.$this->url.'&page='.$this->pages.'">末页</a> '; }else{ return ''; } } //开始 从多少条记录开始 private function start(){ return ($this->cpage-1)*$this->nums+1; } //结束 到多少条记录结束 private function end(){ return min($this->cpage*$this->nums,$this->total); } //当前显示的条数 private function currnum(){ return $this->end()-$this->start()+1; } //分页方法 public function fpage(){ $arr = func_get_args(); $fpage =""; $pages[0]=" 共{$this->total}条记录 "; $pages[1]=" 本页显示".$this->currnum()."条记录 "; $pages[2]=" 从".$this->start()."-".$this->end()."条 "; $pages[3]=" {$this->cpage}/{$this->pages} "; $pages[4]=" ".$this->first()." "; $pages[5]=" ".$this->flist()." "; $pages[6]=" ".$this->last()." "; if(count($arr)<1) $arr = array(0,1,2,3,4,5,6); foreach($arr as $n){ $fpage .= $pages[$n]; } return $fpage; } }
调用分页类
include 'print.php'; $page = new Page(1000,10); echo $page->fpage(6,4,5);
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。