自己分装Php分页类

page.class.PHP

  1. <meta charset="utf8">  
  2. <style>  
  3.     a {  
  4.         display:inline-block;  
  5.         border:1px solid #ccc;  
  6.         text-decoration:none;  
  7.         padding:5px;  
  8.         margin:20px 3px;  
  9.         color:#333;  
  10.     }  
  11.     .apage{  
  12.         background:#ccc;  
  13.     }  
  14.     .disable{  
  15.         border:1px solid #ccc;  
  16.         color:#ccc;  
  17.     }  
  18. </style>  
  19.   
  20. <?php  
  21. require "mysql.class.php";  
  22. class page {  
  23.     private $total;  
  24.     private $number=10;  
  25.     private $pagecount;  
  26.     public $limit;  
  27.     private $url;  
  28.     private $page;  
  29.     function __construct($total,$number,$url) {  
  30.         $this->total=$total;  
  31.         $this->number=$number;  
  32.         $this->pagecount=ceil($this->total/$this->number);  
  33.         $this->getpage();  
  34.         $this->url=$url;  
  35.         $this->limit="limit ".$this->setlimit();  
  36.     }  
  37.     private function getpage(){  
  38.         $this->page=(isset($_GET['page'])&&!empty($_GET['page']))?$_GET['page']:1;  
  39.         if(preg_match('/\D/',$this->page)){  
  40.             $this->page=1;  
  41.         }  
  42.         if($this->page<=0){  
  43.             $this->page=1;  
  44.         }  
  45.         if($this->page > $this->pagecount){  
  46.             $this->page=$this->pagecount;  
  47.         }  
  48.     }  
  49.     private function setlimit(){  
  50.         $start=($this->page-1)*$this->number;  
  51.         return ($start.','.$this->number);  
  52.     }  
  53.   
  54.     private function setnumber($num){  
  55.         $str="";  
  56.         if($this->pagecount<=$num){  
  57.             for($i=1;$i<=$this->pagecount;$i++){  
  58.                 if($this->page==$i){  
  59.                     $str.= "<a href='".$this->url."?page=".$i."' class='apage'>".$i."</a>";  
  60.                 }else{  
  61.                     $str.= "<a href='".$this->url."?page=".$i."'>".$i."</a>";  
  62.                     //echo "<a href='demo1.php?page=".$i ."&number=".$number."'>".$i."</a>";  
  63.                 }  
  64.             }  
  65.         }else{  
  66.             if($this->page<=ceil($num/2)){  
  67.                 for($i=1;$i<=$num;$i++){  
  68.                     //给当前页加一个灰色的背景  
  69.                     if($this->page==$i){  
  70.                         $str.= "<a href='".$this->url."?page=".$i."' class='apage'>".$i."</a>";  
  71.                     }else{  
  72.                         $str.= "<a href='".$this->url."?page=".$i."'>".$i."</a>";  
  73.                     }  
  74.                 }  
  75.             }elseif($this->page>=$this->pagecount-floor($num/2)){  
  76.                 //当前页在后3页时,能实现的效果是,点击后三页的时候,分页不会动  
  77.                 for($i=$this->pagecount-$num-1;$i<=$this->pagecount;$i++){  
  78.                     //给当前页加一个灰色的背景  
  79.                     if($this->page==$i){  
  80.                         $str.= "<a href='".$this->url."?page=".$i."' class='apage'>".$i."</a>";  
  81.                     }else{  
  82.                         $str.= "<a href='".$this->url."?page=".$i."'>".$i."</a>";  
  83.                     }  
  84.                 }  
  85.             }else{  
  86.                 //将当前页放在中间页数时  
  87.                 for($i=$this->page-floor($num/2);$i<=$this->page+floor($num/2);$i++){  
  88.                     if($this->page==$i){  
  89.                         $str.= "<a href='".$this->url."?page=".$i."' class='apage'>".$i."</a>";  
  90.                     }else{  
  91.                         $str.= "<a href='".$this->url."?page=".$i."'>".$i."</a>";  
  92.                     }  
  93.                 }  
  94.             }  
  95.         }  
  96.         return $str;  
  97.     }  
  98.     public function fpage($number=false,$num=5){  
  99.         $str="";  
  100.         if($this->page==1){  
  101.             $str.="<a href='javascript:void(0);' class='disable'>首页</a>";  
  102.             $str.="<a href='javascript:void(0);' class='disable'>上一页</a>";  
  103.         }else{  
  104.             $str.="<a href='".$this->url."?page=1'>首页</a>";  
  105.             $str.="<a href='".$this->url."?page=".($this->page-1)."'>上一页</a>";  
  106.         }  
  107.         if($number){  
  108.             $str.=$this->setnumber($num);  
  109.         }  
  110.         if($this->page==$this->pagecount){  
  111.             $str.="<a href='javascript:void(0);' class='disable'>下一页</a>";  
  112.             $str.="<a href='javascript:void(0);' class='disable'>尾页</a>";  
  113.         }else{  
  114.             $str.="<a href='".$this->url."?page=".($this->page+1)."'>下一页</a>";  
  115.             $str.="<a href='".$this->url."?page=".$this->pagecount."'>尾页</a>";  
  116.         }  
  117.         return $str;  
  118.     }  
  119. }  

2.显示出来
  1. <?php  
  2. include "libs/Smarty.class.php";  
  3. include "page.class.php";  
  4. $smarty=new Smarty();  
  5. //include "mysql.class.php";  
  6. //连接数据库  
  7. $mysql=new Mysql('127.0.0.1','root','root','1408phpe');  
  8. //查询总条数  
  9. $total=$mysql->counts("select * from exam8 ");  
  10. //$sql="select * from exam8 ";  
  11. $page=new page($total,2,'show1.php');  
  12. $sql="select * from exam8 $page->limit";  
  13. $a=$mysql->select_all($sql);  
  14. $smarty->assign('arr',$a);  
  15. $smarty->display('show1.tpl');  
  16. echo"<p align='left'>".$page->fpage(true)."</p>"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值