<?php /** * 防淘宝带省略号的AJAX分页 * @param date * @author hany */ class AjaxPage { private $total; //数据表中总记录数 private $listRows; //每页显示行数 private $limit; //分页参数 private $uri; private $pageNum; //总页数 private $config=array(‘header‘=>"个记录", "prev"=>"上一页", "next"=>"下一页", "first"=>"首 页", "last"=>"尾 页"); private $adjacents =3; //两侧显示的页码数量. /** * 构造函数 * @param unknown $total * @param number $listRows * @param string $pa */ public function __construct($total, $listRows=10, $pa=""){ $this->total=$total; $this->listRows=$listRows; $this->uri=$this->getUri($pa); $this->page=!empty($_GET["page"]) ? intval($_GET["page"]) : 1; //当前页数 $this->pageNum=ceil($this->total/$this->listRows); $this->limit=$this->setLimit(); } /** * 设置分页偏移量 * @return string */ private function setLimit(){ return "Limit ".($this->page-1)*$this->listRows.", {$this->listRows}"; } /** * 获取url地址 * @param unknown $pa * @return string */ private function getUri($pa){ $url=$_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"], ‘?‘)?‘‘:"?").$pa; //解析 URL,返回其组成部分 $parse=parse_url($url); //将字符串解析成多个变量 if(isset($parse["query"])){ parse_str($parse[‘query‘],$params); unset($params["page"]); //去除url里面的page $url=$parse[‘path‘].‘?‘.http_build_query($params); } return $url; } /** * 获取一个类的私有成员 * @param unknown $args * @return NULL */ public function __get($args){ if($args=="limit"){ return $this->limit; }else{ return null; } } /** * 首页 * @return string */ private function first(){ if($this->page >($this->adjacents+1)) { return "<a href=‘javascript:setPage(\"{$this->uri}&page=1\")‘>1</a> "; } return ""; } /** * 上一页 * @return string */ private function prev(){ if($this->page == 1) { return "<a>{$this->config["prev"]}</a> "; }elseif($this->page == 2) { return "<a href=‘javascript:setPage(\"{$this->uri}"."\")‘>{$this->config["prev"]}</a> "; }else { return "<a href=‘javascript:setPage(\"{$this->uri}&page=".($this->page-1)."\")‘>{$this->config["prev"]}</a> "; } } /** * 列表 * @return string */ private function pageList(){ $linkPage = ""; //省略号 if($this->page >($this->adjacents+2)) { $linkPage.= "<a>...</a> "; } // pages $pmin = ($this->page>$this->adjacents) ? ($this->page-$this->adjacents) : 1; $pmax = ($this->page<($this->pageNum-$this->adjacents)) ? ($this->page+$this->adjacents) : $this->pageNum; for($i=$pmin; $i<=$pmax; $i++) { if($i==$this->page) { $linkPage.="<a class=‘current‘>{$this->page}</a> "; }elseif($i==1) { $linkPage.="<a href=‘javascript:setPage(\"{$this->uri}"."\")‘>{$i}</a> "; }else { $linkPage.="<a href=‘javascript:setPage(\"{$this->uri}&page={$i}\")‘>{$i}</a> "; } } //省略号 if($this->page<($this->pageNum-$this->adjacents-1)) { $linkPage.= "<a>...</a> "; } return $linkPage; } /** * 下一页 * @return string */ private function next(){ if($this->page<$this->pageNum) { return "<a href=‘javascript:setPage(\"{$this->uri}&page=".($this->page+1)."\")‘>{$this->config["next"]}</a> "; }else { return "<a>{$this->config["next"]}</a> "; } } /** * 总页数 * @return string */ private function total(){ return ‘共‘.$this->pageNum.‘页,第‘.$this->page.‘页 ‘; } /** * 尾页 * @return string */ private function last(){ if($this->page<($this->pageNum-$this->adjacents)) { return "<a href=‘javascript:setPage(\"{$this->uri}&page=".($this->pageNum)."\")‘>{$this->pageNum}</a> "; } return ""; } /** * 跳转 * @return string */ private function goPage(){ return ‘<input type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>‘.$this->pageNum.‘)?‘.$this->pageNum.‘:this.value;setPage(\‘‘.$this->uri.‘&page=\‘+page+\‘\‘)}" value="‘.$this->page.‘" style="width:25px"><input type="button" value="GO" onclick="javascript:var page=(this.previousSibling.value>‘.$this->pageNum.‘)?‘.$this->pageNum.‘:this.previousSibling.value;setPage(\‘‘.$this->uri.‘&page=\‘+page+\‘\‘)">‘; } /** * 获取分页列表 * @param unknown $display * @return Ambigous <string, unknown> */ public function fpage($display=array(0,1,2,3,4,5)){ $html[0]=$this->total(); $html[1]=$this->prev(); $html[2]=$this->first(); $html[3]=$this->pageList(); $html[4]=$this->last(); $html[5]=$this->next(); $fpage=‘‘; foreach($display as $index){ $fpage.=$html[$index]; } return $fpage; } } //实例分页类对象 $page = new AjaxPage($total,$pagesize);
时间: 2024-10-08 16:09:22