傅令江的光影色彩世界
留住记忆的点滴
  • 首页
  • 文学
    • 诗词鉴赏
    • 美文共读
    • 原创
  • 编程
    • php
    • asp
    • .net
    • VB
    • C/C++
    • 易语言
    • js
    • 其他
    • 逆向
  • 运维
    • windows
    • linux
  • 光影色彩
    • 电影
    • 音乐
  • 科技
    • 互联网
    • 手机
  • 生活
    • 情感
  • 微语
8月232014

php简单通用的分页类

作者:令狐江   发布:2014-8-23 7:37   分类:php   阅读:3646次   评论:0条  
<?php
/**

*/
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 '<span id="firstpage"><a href="'.$this->getUrl(1).'" title="查看第一页">首页</a></span>';
		}else{
			return '<span id="firstpage"><a>首页</a></span>';
		}
	}
	
	//获得上一页链接完整html str
	protected function prevUrl(){
		if($this->current<=1){
			$fullurl='<span id="prevpage"><a>上一页</a></span>';
		}else{
			$fullurl=$this->url.($this->current-1).$this->postfix;
			$fullurl='<span id="prevpage" title="查看上一页"><a href="'.$fullurl.'">上一页</a></span>';
		}
		return $fullurl;
	}
	
	//获得下一页链接完整html str
	protected function nextUrl(){
		if($this->current>=$this->maxPageNum()){
			$fullurl='<span id="nextpage"><a>下一页</a></span>';
		}else{
			$fullurl=$this->url.($this->current+1).$this->postfix;
			$fullurl='<span id="nextpage" title="查看下一页"><a href="'.$fullurl.'">下一页</a></span>';
		}
		return $fullurl;
	}
	
	//获得最后一页链接完整html str
	protected function lastUrl(){
		if($this->current>=$this->maxPageNum()){
			$fullurl='<span id="lastpage"><a>末页</a></span>';
		}else{
			$fullurl=$this->url.$this->maxPageNum().$this->postfix;
			$fullurl='<span id="lastpage" title="最后一页"><a href="'.$fullurl.'">末页</a></span>';
		}
		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.="<a href='{$currenturl}'>{$i}</a>";
			}else{
				$str.='<span id="current"><a>'.$i.'</a></span>';
			}
		}
		return $str;
	}
	
	//返回完整分页html字符串
	public function getPageStr(){
		$str='<div id="pagelist">'.$this->firstUrl().$this->prevUrl();
		$str.=$this->getMiddelUrl();
		$str.=$this->nextUrl().$this->lastUrl().'<span id="allpagesize">共'.$this->maxPageNum().'页'.$this->allnum.'条</span></div>';
		return $str;
	}
}
?>



本文固定链接: https://www.fulingjiang.cn/php/38.html

blogger
该日志由 令狐江 于2014-8-23 7:37 Saturday发表在 php 分类下。
版权所有:《傅令江的光影色彩世界》 → 《php简单通用的分页类》;
除特别标注,本博客所有文章均为原创. 互联分享,尊重版权,转载请以链接形式标明本文地址;
本文标签: 分页 分页函数 分页方法
上一篇::php生成缩略图的类
下一篇:php实时提交百度Sitemap 实时推送代码

热门文章

  • 兄弟二周年祭

相关文章

  • PHP curl伪造ip(用于刷票)
  • thinkphp6 session 为什么会失效
  • PHP自动定时循环执行任务
  • php smtp发送邮件类,带使用示例
  • DWZ(J-ui)框架中分页组件的详细说明
取消回复

发表评论

亲,头像对么?

提交中,请稍候……


木有头像就木JJ啦!还木有头像吗?点这里申请属于你的个性Gravatar头像吧!


  • 日历

  • 存档

    • 2024年10月(1)
    • 2023年2月(1)
    • 2022年11月(1)
    • 2022年10月(10)
    • 2022年9月(13)
    • 2022年8月(2)
    • 2022年7月(14)
    • 2022年6月(2)
    • 2022年5月(8)
    • 2022年4月(7)
    • 2022年3月(13)
    • 2022年2月(2)
    • 2022年1月(9)
    • 2021年12月(2)
    • 2021年11月(4)
    • 2021年10月(2)
    • 2021年9月(6)
    • 2021年7月(4)
    • 2021年6月(3)
    • 2021年5月(3)
    • 2021年4月(11)
    • 2021年3月(13)
    • 2021年2月(2)
    • 2021年1月(1)
    • 2020年12月(1)
    • 2020年4月(5)
    • 2019年9月(1)
    • 2019年8月(1)
    • 2019年5月(3)
    • 2018年3月(1)
    • 2017年10月(1)
    • 2016年7月(1)
    • 2016年4月(1)
    • 2015年12月(1)
    • 2015年11月(3)
    • 2015年9月(1)
    • 2015年8月(10)
    • 2015年7月(1)
    • 2015年6月(1)
    • 2015年4月(1)
    • 2015年3月(3)
    • 2015年2月(8)
    • 2015年1月(4)
    • 2014年12月(1)
    • 2014年11月(27)
    • 2014年10月(13)
    • 2014年9月(14)
    • 2014年8月(26)
    • 2014年7月(21)
  • 最新评论

    • 令狐江:
      喜欢这首歌是因为可以引起共鸣!
  • 链接

    • 演讲稿网
    • Recollect
    • 演讲稿
    • 祁阳人生活网
    • 我爱演讲稿网
  • 搜索

  • 标签

      函数 自定义方法 SEO 分页 分页函数 分页方法 nginx重新的一些规则
  • 分类

    • 文学(0)
    • 编程(0)
    • 运维(0)
    • 光影色彩(0)
    • 科技(0)
    • 生活(0)
    • 诗词鉴赏(3)
    • 美文共读(1)
    • 原创(10)
    • php(111)
    • asp(1)
    • .net(0)
    • VB(0)
    • C/C++(0)
    • 易语言(0)
    • js(8)
    • 其他(9)
    • 逆向(2)
    • windows(11)
    • linux(121)
    • 电影(0)
    • 音乐(1)
    • 互联网(4)
    • 手机(0)
    • 情感(2)
  • 最新文章热门文章随机文章

    • 兄弟二周年祭
    • openai给的ionCube 解密代码,应该是老版本可以这样
    • WordPress – 5秒盾防CC(PHP通用代码)
    • 我高中最好的朋友今天猝死了-伤心得不行
    • Linux系统中 systemd-journaldCPU占用异常的解决方法
    • SVN Skipped 'xxx' -- Node remains in conflict 错误的解决办法
    • 解决Linux读写nfs共享盘速度慢的问题
    • php 获取302跳转后的地址
    • 让vsftp显示隐藏文件的办法,比如显示 .htaccess
    • Nginx负载均衡配置实例详解
    • nginx如何批量反向代理二级域名
    • 批量二级域名对应重定向跳转
    • proxy_pass url 反向代理的坑
    • 通过模板替换生成html文件,mvp其实效率不高的,一个人开发原生最快
    • 一键解决BT宝塔面板的强制登陆限制要求教程
Copyright © 2001-2025 傅令江的光影色彩世界. Powered by www.fulingjiang.cn ICP备案:京ICP备14015190号-5