class Page{ private $total; private $listRows; private $limit; private $pageNum; private $config = array("header"=>"个记录","prev" =>"上一页","next"=>"下一页","first"=>"首页","last"=>"尾页"); private $listNum = 8; //显示的最大页数 //$pa地址传参cid = 99 public function __construct($total,$listRows=10,$pa ="",$listNum,$config){ $this->total = $total; $this->listRows = $listRows; $this->uri =$this->getUri($pa); $this->listNum = $listNum; $this->config = $config; $this->page = !empty($_GET['page'])? $_GET['page']:1; $this->pageNum = ceil($this->total/$this->listRows); $this->limit =$this->setLimit(); } public function __get($args){ if($args=="limit"){ return $this->limit; }else{ return null; } } private function setLimit(){ return "Limit ".($this->page-1)*$this->listRows.", {$this->listRows}"; } private function getUri($pa){ /* $_SERVER['REQUEST_URI'] 域名后面的部分(除了'/') strpos() 函数返回字符串在另一个字符串中第一次出现的位置。如果存在 ? 返回 '' 不然后面加上 ? 与含 '&cid =99' 的字符串相连组成一个字符串 parse_url() 将字符串解析成数组 $parse parse_atr() 会将$parse['query']?(?后面的参数)组合成一个数组 $params;销毁数组中的 page,$parse数组中的path与$params数组用http_build_query()函数生成一个URL-encode的字符串,返回没有page参数的url */ $url = $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')? '' : "?").$pa; $parse = parse_url($url); if(isset($parse['query'])){ parse_str($parse['query'],$params); unset($params['page']); ///http_build_query() 使用给出的关联(或下标)数组生成一个经过 URL-encode 的请求字符串 $url =$parse['path'].'?'.http_build_query($params); } return $url; } private function start(){ if($this->total ==0) return 0; else return ($this->page -1)*$this->listRows +1; } private function end(){ return min($this->page*$this->listRows,$this->total); } private function first(){ if($this->page == 1){ //$html .="<a>{$this->config['first']}</a>"; }else{ $html .= "<a href='{$this->uri}&page=1'>{$this->config["first"]}</a>"; } return $html; } private function prev(){ if($this->page ==1){ //$html .="<a >{$this->config['prev']}</a>"; }else{ $html .="<a href='{$this->uri}&page=".($this->page -1)."'>{$this->config["prev"]}</a>"; } return $html; } private function pageList(){ $linkPage = ''; $inum =floor($this->listNum/2); for($i =$inum; $i>=1 ;$i--){ $page = $this->page-$i; if($page < 1){ continue; } $linkPage .="<a href='{$this->uri}&page={$page}'>{$page}</a>"; } $linkPage .="<a class='current'>{$this->page}</a>"; for($i= 1;$i <= $inum ;$i++){ $page = $this->page +$i; if($page <$this->pageNum){ $linkPage .="<a href='{$this->uri}&page={$page}'>{$page}</a>"; }else break; } return $linkPage; } private function next(){ if($this->page == $this->pageNum){ //$html .="<a>{$this->config["next"]}</a>"; }else{ $html .="<a href='{$this->uri}&page=".($this->page+1)."'>{$this->config["next"]}</a>"; } return $html; } private function last(){ if($this->page == $this->pageNum){ //$html .="<a >{$this->config["last"]}</a>"; }else{ $html .= "<a href='{$this->uri}&page=".($this->pageNum)."'>{$this->config["last"]}</a>"; } return $html; } function fpage($display = array(0,1,2,3,4,5,6,7,8)){ /* $html[0]= "共有<b>{$this->total}</b>{$this->config["header"]}"; $html[1]= "每页显示<b>".($this->end()-$this->start()+1)."</b>条,本页<b>{$this->start}-{$this->end()}</b>条"; $html[2]= "<b>{$this->page}/{$tihs->pageNum}</b>页"; */ $html[3]= $this->first(); $html[4]= $this->prev(); $html[5]= $this->pageList(); $html[6]= $this->next(); $html[7]= $this->last(); //$html[8]= $this->goPage(); $fpage = ""; foreach($display as $index){ $fpage .=$html[$index]; } return $fpage; } }