ROOT:
├─conn
│ └─conn.php
│
├─libs -- smarty库
│
├─templates
│ │
│ ├─demo.html -- 功能页模板文件
│ │
│ ├─css
│ │ ├─common.css
│ │ └─style1.css
│ │
│ ├─images
│ │ └─loading.gif -- ajax分页时请求数据接收到之前的加载图
│ └─js
│ ├─jquery-1.8.3.min.js
│ └─ajax.js -- 当分页方式为ajax时模板demo.html加载的js
│
├─templates_c
│
├─init.inc.php -- smarty配置文件
│
├─page.class.php -- 分页类
│
├─demo.php
│
└─ajaxpage.php -- ajax分页时接受请求的php文件
要注意的地方:
1.偏移量的显示设置,主要是什么时候 url 分页方式1,什么时候显示"..." :当前页码 - 前偏移量 - 1 > 1 时,应当显示前面的"..."; 当前页码 + 后偏移量 + 1 < 总页数时,应当显示后面的"...";
2.选择性加载 js :当使用 ajax 方式进行分页时,才加载 ajax.js
3.外部的 js 无法解析 smarty 的标签,所以在使用外部 js 时的传值要进行处理
4.ajax 分页时,默认是第一页,也就是一定首先会出现 "下一页" 和 "末页",所以 "上一页" 和 "首页" 的添加和点击函数应当包含在"下一页" 和 "末页" 的点击函数中。
主要代码:
page.class.php:
<?php class MyPageUrl{ private $totalNum; private $perpageNum; //每页显示条数 private $pageNow; //当前页页码 private $url; //当前url //页码显示 private $pageStyle; //页码样式,提供2种样式 private $prePage; //页码前偏移量 private $floPage; //页码后偏移量 private $skipStyle; //手动跳转,0为手动输入页码,1为下拉菜单选择页码 private $page_act; //翻页样式 0:url 1:ajax //页码文字 //style2&style3 private $firstFonts = "首页"; private $lastFonts = "末页"; private $nextFonts = "下一页 >"; private $preFonts = "< 上一页"; //前n页,后n页 private $page_n; private $pn = 10; private $pn_fonts = "前10页"; private $fn = 10; private $fn_fonts = "后10页"; //展现的页码 private $pageShow; //构造函数 function __construct($totalNum,$perpageNum,$prePage,$preFonts,$floPage,$nextFonts,$p,$skipStyle,$pageStyle,$page_n,$page_act){ $this->totalNum = $totalNum; $this->perpageNum = $perpageNum; $this->prePage = $prePage; $this->floPage = $floPage; $this->skipStyle = $skipStyle; $this->pageStyle = $pageStyle; $this->page_n = $page_n; $this->page_act = $page_act; $this->getPageNow($p); $this->totalPage = $this->getTotalPage(); //总页数 $this->pageShow = ""; $this->getUrl(); } /************定义__toString方法,把对象解析成字符串******/ public function __toString(){ return $this->pageShow; } /************获得当前页页码,$p用来接收$_GET['p']*******/ public function getPageNow($p){ if(!isset($p)){ $this->pageNow = 1; }else if($p>0){ $this->pageNow = $p; }else{ die("page number error"); } return $this->pageNow; } /***********************设置当前页面链接***************/ public function getUrl(){ $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; //判断是否带参数 if(strpos($url,"?") === false){ //不带参数 return $this->url = $url."?"; }else{ //带参数 $url = explode("?",$url); //参数 $param = $url[1]; //判断是否有多个参数 if(strpos($param,"&") === false){ //只有一个参数 //判断参数是否为p if(strpos($param,"p=") === false){ //不含参数p //合并url $url = implode("?",$url); return $this->url = $url."&"; }else{ //把参数p去掉 $url = $url[0]; return $this->url = $url."?"; } }else{ //多个参数 $param = explode("&",$param); //遍历参数数组 foreach($param as $k=>$v){ if(strpos($v,"p=") === false){ continue; }else{ //当含有参数p时,把它从数组中删除 unset($param[$k]); } } //删除参数p之后组合数组 $param = implode("&",$param); $url[1] = $param; $url = implode("?",$url); return $this->url = $url."&"; } } } /************************前偏移量处理********************/ public function preOffset($preFonts){ //前偏移量的处理 if($this->pageNow!=1 && ($this->pageNow - $this->prePage -1 <= 1)){ //上一页 $this->pageShow .= "<a id=https://www.jb51.net/article/\"per_page\" class=https://www.jb51.net/article/\"pagenum\" href=https://www.jb51.net/article/\"".$this->url."p=".($this->pageNow-1)."https://www.jb51.net/article/\">".($preFonts == ""?$this->preFonts:$preFonts)."</a>"; //页码 for($i=1;$i<=$this->pageNow-1;$i++){ //ajax方式不显示 if($this->page_act != 1){ $this->pageShow .= "<a class=https://www.jb51.net/article/\"pagenum\" href=https://www.jb51.net/article/\"".$this->url."p=".$i."https://www.jb51.net/article/\">".$i."</a>"; } } }else if($this->pageNow - $this->prePage -1 > 1){ //pageNow至少大于2时才会出现"1..." //样式1.加上'首页' if($this->pageStyle == 2 || $this->page_act == 1){ //首页 $this->pageShow .= "<a id=https://www.jb51.net/article/\"first_page\" class=https://www.jb51.net/article/\"pagenum\" href=https://www.jb51.net/article/\"".$this->url."p=1\">".$this->firstFonts."</a>"; //style2.前n页 if($this->page_n == 1 && $this->page_act != 1){ if($this->pageNow>$this->pn){ $this->pageShow .= "<a id=https://www.jb51.net/article/\"pre_n_page\" class=https://www.jb51.net/article/\"pagenum\" href=https://www.jb51.net/article/\"".$this->url."p=".($this->pageNow-$this->pn)."https://www.jb51.net/article/\">".$this->pn_fonts."</a>"; } } } //上一页 $this->pageShow .= "<a id=https://www.jb51.net/article/\"pre_page\" class=https://www.jb51.net/article/\"pagenum\" href=https://www.jb51.net/article/\"".$this->url."p=".($this->pageNow-1)."https://www.jb51.net/article/\">".($preFonts == ""?$this->preFonts:$preFonts)."</a>"; //样式1.加上第一页'1'、'...' if($this->pageStyle == 1){ $this->pageShow .= "<a class=https://www.jb51.net/article/\"pagenum\" href=https://www.jb51.net/article/\"".$this->url."https://www.jb51.net/article/\">1</a><a id=https://www.jb51.net/article/\"pre_page_2\" class=https://www.jb51.net/article/\"pagenum\" href=https://www.jb51.net/article/\"".$this->url."p=".($this->pageNow-$this->prePage-1)." \" title=https://www.jb51.net/article/\"第".($this->pageNow-$this->prePage-1)."页\">…</a>"; } for($i=$this->prePage;$i>=1;$i--){ //当前页和'...'之间的页码,ajax方式不显示 if($this->page_act != 1){ $this->pageShow .= "<a class=https://www.jb51.net/article/\"pagenum\" href=https://www.jb51.net/article/\"".$this->url."p=".($this->pageNow-$i)."https://www.jb51.net/article/\">".($this->pageNow-$i)."</a>"; } } } } /**********************页码和后偏移量处理***************************/ public function floOffset($nextFonts){ if($this->totalPage > $this->floPage){ //总页数大于后偏移量时 for($i=0;$i<=$this->floPage;$i++){ $page = $this->pageNow+$i; if($page<=$this->totalPage){ //页码,ajax方式不显示 if($this->page_act != 1){ $this->pageShow .= "<a class=https://www.jb51.net/article/\"pagenum\" href=https://www.jb51.net/article/\"".$this->url."p=".$page."https://www.jb51.net/article/\">".$page."</a>"; } } } if($this->pageNow < $this->totalPage){ //当前页+后偏移量+1小于总页数时出现"..." if(($this->pageNow+$this->floPage+1)<$this->totalPage){ //样式1.显示'...' if($this->pageStyle == 1){ $this->pageShow .= "<a id=https://www.jb51.net/article/\"flo_page_2\" class=https://www.jb51.net/article/\"pagenum\" href=https://www.jb51.net/article/\"".$this->url."p=".($page+1)."https://www.jb51.net/article/\" title=https://www.jb51.net/article/\"第".($page+1)."页\">…</a>"; } } //当前页+后偏移量+1小于等于总页数时出现最后一页的快捷标签 if(($this->pageNow+$this->floPage+1)<=$this->totalPage){ //最后一页 //样式1.始终出现'最后一页页码' if($this->pageStyle == 1){ $this->pageShow .= "<a class=https://www.jb51.net/article/\"pagenum\" href=https://www.jb51.net/article/\"".$this->url."p=".$this->totalPage."https://www.jb51.net/article/\" title=https://www.jb51.net/article/\"总共".$this->totalPage."页\">".$this->totalPage."</a>"; } } $this->pageShow .= "<a id=https://www.jb51.net/article/\"flo_page\" class=https://www.jb51.net/article/\"pagenum\" href=https://www.jb51.net/article/\"".$this->url."p=".($this->pageNow+1)."https://www.jb51.net/article/\">".($nextFonts == ""?$this->nextFonts:$nextFonts)."</a>"; //当实例化对象时用户传递的文字为空时则调用类预设的"下一页",否则输出用户传递的值 //style2.加上后n页 if($this->pageStyle == 2 && $this->page_n == 1 && $this->page_act != 1){ if(($this->pageNow+10)<$this->totalPage){ $this->pageShow .= "<a id=https://www.jb51.net/article/\"flo_n_page\" class=https://www.jb51.net/article/\"pagenum\" href=https://www.jb51.net/article/\"".$this->url."p=".($this->pageNow+$this->fn)."https://www.jb51.net/article/\">".$this->fn_fonts."</a>"; } } //显示'末页' if($this->pageStyle == 2){ if(($this->pageNow+$this->floPage+1)<$this->totalPage){ $this->pageShow .= "<a id=https://www.jb51.net/article/\"last_page\" class=https://www.jb51.net/article/\"pagenum\" href=https://www.jb51.net/article/\"".$this->url."p=".$this->totalPage."https://www.jb51.net/article/\">末页</a>"; } } }else if($this->pageNow > $this->totalPage){ die("超出页码范围"); } }else{ //总页数小于后偏移量时 for($i=0;$i<$this->totalPage;$i++){ $page = $this->pageNow+$i; //页码后边界 $this->pageShow .= "<a class=https://www.jb51.net/article/\"pagenum\" href=https://www.jb51.net/article/\"".$this->url."p=".$page."https://www.jb51.net/article/\">".$page."</a>"; } } } /********************其它页面信息***********************/ public function getOtherInfo(){ //ajax方式不显示手动跳转信息 if($this->page_act != 1){ $this->pageShow .= " 跳转至 "; //跳转类型 if($this->skipStyle =="" ){ //不加跳转 $this->pageShow .= ""; }else if($this->skipStyle == 1){ //输入框 $this->pageShow .= "<input id=https://www.jb51.net/article/\"skip\" type=https://www.jb51.net/article/\"text\" value=https://www.jb51.net/article/\"".$this->pageNow."https://www.jb51.net/article/\">"; $this->pageShow .= "<button id=https://www.jb51.net/article/\"go\">GO</button>"; }else if($this->skipStyle == 2){ //下拉菜单 //选择下拉菜单自动跳转 $this->pageShow .= "<select id=https://www.jb51.net/article/\"select_page\" onchange=https://www.jb51.net/article/\"location.href=this.options[this.selectedIndex].value;\" >"; for($i=1;$i<=$this->totalPage;$i++){ $this->pageShow .= "<option value=https://www.jb51.net/article/\"".$this->url."p=".$i."https://www.jb51.net/article/\""; //下拉菜单默认显示当前页 if($i == $this->pageNow){ $this->pageShow .= " selected"; } $this->pageShow .= ">".$i."</option>"; } $this->pageShow .= "</select>"; } } $this->pageShow .= "<span id=https://www.jb51.net/article/\"pagenow_info\"> 当前第".$this->pageNow."页</span>"; $this->pageShow .= "/<span id=https://www.jb51.net/article/\"totalpage_info\">共".$this->totalPage."页</span>"; $this->pageShow .= " <span id=https://www.jb51.net/article/\"totalNum_info\">共".$this->totalNum."条</span>"; } /*****************获取每页第一条信息*****************/ public function getFirstRow(){ $firstRow = $this->perpageNum * ($this->pageNow-1) + 1;//当前页第一条是总条数中第几条 return $firstRow; } /********************获得总页数***********************/ public function getTotalPage(){ $totalPage = ceil($this->totalNum / $this->perpageNum); return $totalPage; } /* ****************获取上一页、下一页文字*************** */ public function getPreFonts($preFonts){ return ($preFonts=="")?$this->preFonts:$preFonts; } public function getNextFonts($nextFonts){ return ($nextFonts=="")?$this->nextFonts:$nextFonts; } }
demo.php: