php分页类 潇湘博客

  1. <?php  
  2. class SubPages{  
  3.     
  4.    private  $each_disNums;//每页显示的条目数  
  5.   private  $nums;//总条目数  
  6.   private  $current_page;//当前被选中的页  
  7.   private  $sub_pages;//每次显示的页数  
  8.   private  $pageNums;//总页数  
  9.   private  $page_array = array();//用来构造分页的数组  
  10.   private  $subPage_link;//每个分页的链接  
  11.   private  $subPage_type;//显示分页的类型  
  12.    /* 
  13.    __construct是SubPages的构造函数,用来在创建类的时候自动运行. 
  14.    @$each_disNums   每页显示的条目数 
  15.    @nums     总条目数 
  16.    @current_num     当前被选中的页 
  17.    @sub_pages       每次显示的页数 
  18.    @subPage_link    每个分页的链接 
  19.    @subPage_type    显示分页的类型 
  20.    
  21.    当@subPage_type=1的时候为普通分页模式 
  22.          example:   共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页] 
  23.          当@subPage_type=2的时候为经典分页样式 
  24.          example:   当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页] 
  25.    */  
  26.   function __construct($each_disNums,$nums,$current_page,$sub_pages,$subPage_link,$subPage_type){  
  27.    $this->each_disNums=intval($each_disNums);  
  28.    $this->nums=intval($nums);  
  29.     if(!$current_page){  
  30.     $this->current_page=1;  
  31.     }else{  
  32.     $this->current_page=intval($current_page);  
  33.     }  
  34.    $this->sub_pages=intval($sub_pages);  
  35.    $this->pageNums=ceil($nums/$each_disNums);  
  36.    $this->subPage_link=$subPage_link;   
  37.    $this->show_SubPages($subPage_type);   
  38.    //echo $this->pageNums."--".$this->sub_pages;  
  39.   }  
  40.     
  41.     
  42.   /* 
  43.     __destruct析构函数,当类不在使用的时候调用,该函数用来释放资源。 
  44.    */  
  45.   function __destruct(){  
  46.     unset($each_disNums);  
  47.     unset($nums);  
  48.     unset($current_page);  
  49.     unset($sub_pages);  
  50.     unset($pageNums);  
  51.     unset($page_array);  
  52.     unset($subPage_link);  
  53.     unset($subPage_type);  
  54.    }  
  55.     
  56.   /* 
  57.     show_SubPages函数用在构造函数里面。而且用来判断显示什么样子的分页   
  58.    */  
  59.   function show_SubPages($subPage_type){  
  60.     if($subPage_type == 1){  
  61.     $this->subPageCss1();  
  62.     }elseif ($subPage_type == 2){  
  63.     $this->subPageCss2();  
  64.     }  
  65.    }  
  66.     
  67.     
  68.   /* 
  69.     用来给建立分页的数组初始化的函数。 
  70.    */  
  71.   function initArray(){  
  72.     for($i=0;$i<$this->sub_pages;$i++){  
  73.     $this->page_array[$i]=$i;  
  74.     }  
  75.     return $this->page_array;  
  76.    }  
  77.     
  78.     
  79.   /* 
  80.     construct_num_Page该函数使用来构造显示的条目 
  81.     即使:[1][2][3][4][5][6][7][8][9][10] 
  82.    */  
  83.   function construct_num_Page(){  
  84.     if($this->pageNums < $this->sub_pages){  
  85.     $current_array=array();  
  86.      for($i=0;$i<$this->pageNums;$i++){   
  87.      $current_array[$i]=$i+1;  
  88.      }  
  89.     }else{  
  90.     $current_array=$this->initArray();  
  91.      if($this->current_page <= 3){  
  92.       for($i=0;$i<count($current_array);$i++){  
  93.       $current_array[$i]=$i+1;  
  94.       }  
  95.      }elseif ($this->current_page <= $this->pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1 ){  
  96.       for($i=0;$i<count($current_array);$i++){  
  97.       $current_array[$i]=($this->pageNums)-($this->sub_pages)+1+$i;  
  98.       }  
  99.      }else{  
  100.       for($i=0;$i<count($current_array);$i++){  
  101.       $current_array[$i]=$this->current_page-2+$i;  
  102.       }  
  103.      }  
  104.     }  
  105.      
  106.     return $current_array;  
  107.    }  
  108.     
  109.   /* 
  110.    构造普通模式的分页 
  111.    共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页] 
  112.    */  
  113.   function subPageCss1(){  
  114.    $subPageCss1Str="";  
  115.    $subPageCss1Str.="共".$this->nums."条记录,";  
  116.    $subPageCss1Str.="每页显示".$this->each_disNums."条,";  
  117.    $subPageCss1Str.="当前第".$this->current_page."/".$this->pageNums."页 ";  
  118.     if($this->current_page > 1){  
  119.     $firstPageUrl=$this->subPage_link."1";  
  120.     $prewPageUrl=$this->subPage_link.($this->current_page-1);  
  121.     $subPageCss1Str.="[<a href='$firstPageUrl'>首页</a>] ";  
  122.     $subPageCss1Str.="[<a href='$prewPageUrl'>上一页</a>] ";  
  123.     }else {  
  124.     $subPageCss1Str.="[首页] ";  
  125.     $subPageCss1Str.="[上一页] ";  
  126.     }  
  127.      
  128.     if($this->current_page < $this->pageNums){  
  129.     $lastPageUrl=$this->subPage_link.$this->pageNums;  
  130.     $nextPageUrl=$this->subPage_link.($this->current_page+1);  
  131.     $subPageCss1Str.=" [<a href='$nextPageUrl'>下一页</a>] ";  
  132.     $subPageCss1Str.="[<a href='$lastPageUrl'>尾页</a>] ";  
  133.     }else {  
  134.     $subPageCss1Str.="[下一页] ";  
  135.     $subPageCss1Str.="[尾页] ";  
  136.     }  
  137.      
  138.     echo $subPageCss1Str;  
  139.      
  140.    }  
  141.     
  142.     
  143.   /* 
  144.    构造经典模式的分页 
  145.    当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页] 
  146.    */  
  147.   function subPageCss2(){  
  148.    $subPageCss2Str="";  
  149.    $subPageCss2Str.="当前第".$this->current_page."/".$this->pageNums."页 ";  
  150.      
  151.      
  152.     if($this->current_page > 1){  
  153.     $firstPageUrl=$this->subPage_link."1";  
  154.     $prewPageUrl=$this->subPage_link.($this->current_page-1);  
  155.     $subPageCss2Str.="[<a href='$firstPageUrl'>首页</a>] ";  
  156.     $subPageCss2Str.="[<a href='$prewPageUrl'>上一页</a>] ";  
  157.     }else {  
  158.     $subPageCss2Str.="[首页] ";  
  159.     $subPageCss2Str.="[上一页] ";  
  160.     }  
  161.      
  162.    $a=$this->construct_num_Page();  
  163.     for($i=0;$i<count($a);$i++){  
  164.     $s=$a[$i];  
  165.      if($s == $this->current_page ){  
  166.      $subPageCss2Str.="[<span style='color:red;font-weight:bold;'>".$s."</span>]";  
  167.      }else{  
  168.      $url=$this->subPage_link.$s;  
  169.      $subPageCss2Str.="[<a href='$url'>".$s."</a>]";  
  170.      }  
  171.     }  
  172.      
  173.     if($this->current_page < $this->pageNums){  
  174.     $lastPageUrl=$this->subPage_link.$this->pageNums;  
  175.     $nextPageUrl=$this->subPage_link.($this->current_page+1);  
  176.     $subPageCss2Str.=" [<a href='$nextPageUrl'>下一页</a>] ";  
  177.     $subPageCss2Str.="[<a href='$lastPageUrl'>尾页</a>] ";  
  178.     }else {  
  179.     $subPageCss2Str.="[下一页] ";  
  180.     $subPageCss2Str.="[尾页] ";  
  181.     }  
  182.     echo $subPageCss2Str;  
  183.    }  
  184. }  
  185. ?> 

  1. <?php  
  2. require_once("SubPages.php");  
  3. //每页显示的条数  
  4.   $page_size=20;  
  5. //总条目数  
  6.   $nums=1024;  
  7. //每次显示的页数  
  8.   $sub_pages=10;  
  9. //得到当前是第几页  
  10.   $pageCurrent=$_GET["p"];  
  11.   //if(!$pageCurrent) $pageCurrent=1;  
  12.     
  13.   $subPages=new SubPages($page_size,$nums,$pageCurrent,$sub_pages,"test.php?p=",2);  
  14. ?> 

转载:http://www.samchug.com/PHP/12183#

原文地址:https://www.cnblogs.com/fengju/p/6173994.html