个人写的一个简单通用分页类
时间:2021-07-01 10:21:17
帮助过:4人阅读
个人学习php所写简单分页类,通用性比较强
- /**
- * 简单分页类
- * @author:phpprince
- * @QQ: 8923052
- * @date: 2014-04-22 21:08:35
- */
- class Page{
- protected $url; //地址
- protected $allnum; //总记录数
- protected $current; //当前页码
- protected $pagesize; //每页显示多少条记录
- protected $postfix; //后缀
- protected $style; //显示样式 共3种样式,1 2 3 分别表示前5后4,前3后3,前4后4
-
- public function __construct($url,$allnum,$current,$pagesize=10,$postfix='',$style=1){
- $this->url=$url;
- $this->allnum=$allnum;
- $this->current=$current;
- $this->pagesize=$pagesize;
- $this->postfix=$postfix;
- $this->style=$style;
- }
-
- //获取总页数
- protected function maxPageNum(){
- $max=ceil($this->allnum/$this->pagesize);
- //页码超限校正
- if($this->current>$max){
- $this->current=$max;
- }
- if($this->current<1){
- $this->current=1;
- }
- return $max;
- }
-
- //获得第一页链接完整html str
- protected function firstUrl(){
- if($this->current!=1)
- {
- return 'getUrl(1).'" title="查看第一页">首页';
- }else{
- return '首页';
- }
- }
-
- //获得上一页链接完整html str
- protected function prevUrl(){
- if($this->current<=1){
- $fullurl='上一页';
- }else{
- $fullurl=$this->url.($this->current-1).$this->postfix;
- $fullurl='上一页';
- }
- return $fullurl;
- }
-
- //获得下一页链接完整html str
- protected function nextUrl(){
- if($this->current>=$this->maxPageNum()){
- $fullurl='下一页';
- }else{
- $fullurl=$this->url.($this->current+1).$this->postfix;
- $fullurl='下一页';
- }
- return $fullurl;
- }
-
- //获得最后一页链接完整html str
- protected function lastUrl(){
- if($this->current>=$this->maxPageNum()){
- $fullurl='末页';
- }else{
- $fullurl=$this->url.$this->maxPageNum().$this->postfix;
- $fullurl='末页';
- }
- return $fullurl;
- }
-
- //获得指定页码完整url
- protected function getUrl($pageno){
- return $this->url.$pageno.$this->postfix;
- }
-
- //指定显示样式
- protected function getStyle(){
- switch($this->style){
- case 1:
- $before=5;
- $after=4;
- break;
- case 2:
- $before=3;
- $after=3;
- break;
- case 3:
- $before=4;
- $after=4;
- break;
- default :
- $before=5;
- $after=4;
- }
-
- return array($before,$after);
- }
-
- //获得中间URL 1 2 3 4 5 ⑥ 7 8 9 10
- protected function getMiddelUrl(){
- $max=$this->maxPageNum(); //先获取总页,可校正当前页超限问题
- $current=$this->current; //当前页码必须合法,才能保证下面的页码范围一定正确
- //得到当前样式
- list($before,$after)=$this->getStyle();
- $startno=$current-$before; //起始页码
- $endno=$current+$after; //终止页码
-
- //为保证输出始终符合要求,在页面不超限前提下,起始页增加,则终止页必须增加,反之同理.
- while($endno>$max||$startno<1){
- if($endno>$max){ //终止页码超界
- $endno--;
- if($startno>1){
- $startno--;
- }
- }
- if($startno<1){ //起始页码超界
- $startno++;
- if($endno<$max){
- $endno++;
- }
- }
- }
- $str=''; //用于保存整个html str
- for($i=$startno;$i<=$endno;$i++){
- $currenturl=$this->getUrl($i);
- if($i!=$current){
- $str.="{$i}";
- }else{
- $str.=''.$i.'';
- }
- }
- return $str;
- }
-
- //返回完整分页html字符串
- public function getPageStr(){
- $str=''.$this->firstUrl().$this->prevUrl();
- $str.=$this->getMiddelUrl();
- $str.=$this->nextUrl().$this->lastUrl().'共'.$this->maxPageNum().'页'.$this->allnum.'条';
- return $str;
- }
- }
- ?>
|