Chinaunix首页 | 论坛 | 博客
  • 博客访问: 305444
  • 博文数量: 111
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 707
  • 用 户 组: 普通用户
  • 注册时间: 2013-02-26 11:00
个人简介

小伙向前冲呀,小伙向前冲呀。

文章分类

全部博文(111)

文章存档

2014年(43)

2013年(68)

我的朋友

分类: PHP

2013-03-16 14:05:47

简单实用的php分页类(经典推荐),多种分页格式,错过了,便永远错过了,哈哈。

点击(此处)折叠或打开

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

点击(此处)折叠或打开

  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. ?>
阅读(4696) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~