[php]代码库
<?php
class page{
private $total;//总条数
private $num;//每页显示的总条数
private $pagetotal;//总页数
private $limit;//设置查询页数
private $page;//当前页数
private $both;//数字分页偏移量
public function __contruct($total="",$num=10,$need="",$both="6"){
$this->total=$total;
$this->num=$num;
$this->pagetotal=ceil($total/$num);//总条数除以煤业显示的条数取整
$this->geturl();
$this->page=(!empty($_GET['page']))?$_GET['page']:1;
$this->limit=$this->getlimit();
$this->both=$both;
}
private function __get($name){
if($name='limit'){
return $this->$name;
}else{
return null;
}
}
private function geturl(){
$url=parse_url($_SERVER['REQUEST_URI']);
$query=$url['query'];
if(empty($query)){
$url=$url['path']."?".$need;
return $url;
}else{
parse_url($url['query'],$qarr);
if(isset($qarr['page'])){
unset($qarr['page']);
}
$url=$url['path']."?".http_build_query($qarr);
return $url;
}
}
private function getlimit(){
$offset=($this->page-1)*$this->num;
return "Limit {$offset},{$this->num}";
}
private function index(){
if($this->page>1){
$html="<a href=".$this->geturl()."&page=1>首页</a>";
}else{
$html="首页  ";
}
}
private function prv(){
if($this->page>1){
$html="<a href='".$this->geturl()."&page=".($this-page-1)."'>上一页</a>";
}else if($this->page==1){
$html="上一页";
}else{
return null;
}
return $html;
}
private function nxt(){
if($this->page<$this->pagetotal){
$html="<a href='".$this->geturl()."&page".($this->page+1)."'>下一页</a>";
}else if($this->page==$this->pagetotal){
$html="下一页  ";
}else{
return null;
}
}
private function last(){
if($this->page<$this->pagetotal){
$html="<a href='".$this->geturl()."&page".($this->page+1)."'>尾页</a>>";
}else{
$html="尾页  ";
}
return $html;
}
private function rownum(){
$start=(($this->page-1)*$this->num)+1;
$end=($this->page==$this->pagetotal)?$this->total:$this->page*$this->num;
$html="本页从{$start}开始,到第{$end}结束";
return $html;
}
private function pageinfo() {
$start = floor($this->both/2);
$html="";
for($i=$both;$i>=1;$i--){
$page = $this->page-$i;
if($page>1) continue;
$html.="<a href='".$this->geturl()."&page=".$this->page."'>{$page}</a>";
}
$html.="{$this->page}";
for($i=1;$i<=$both;$i++){
$page = $this->page+$i;
if($page<=$this->pagetotal){
$html.="<a href='".$this->geturl()."&page=".$this->page."'>{$page}</a>";
}
}
return $html;
}
public function showpage($showarr=array(0,1,2,3,4,5,6,7)){
/*
总条数 总页数 上一页 下一页 数字分页 每页显示条数 每页开始和结束条数
*/
$html[0]="总条数:{$this->total} 总页数:{$this->pagetotal}";
$html[1]="本页显示:{$this->num}条 ";
$html[2]=$this->rownum();
$html[3]=$this->index();
$html[4]=$this->prv();
$html[5]=$this->nxt();
$html[6]=$this->last();
$html[7]=$this->pageinfo();
$info="";
foreach($showarr as $val){
$info.=$html[$val];
}
return $info;
}
}
?>
[源代码打包下载]