PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > PHP教程 > 正文

    分享一个php分页类

    作者:admin来源:iteye浏览:时间:2020-09-30 00:07:50我要评论
    导读:做项目,数据分页是必不可少的,今天总结一下,把代码分享出来,网友们可以提出修改建议。
    做项目,数据分页是必不可少的,今天总结一下,把代码分享出来,网友们可以提出修改建议。

     
    复制代码代码如下:
    1. <!--?php 
    2.   
    3.  class Page {
    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.      /** 
    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 Page($each_disNums, $nums, $current_page, $sub_pages) {
    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 =$_SERVER['PHP_SELF']."?page=";;
    37.      }
    38.      /** 
    39.       * 照顾低版本 
    40.       */
    41.      /*function __construct($each_disNums, $nums, $current_page, $sub_pages, $subPage_linke) { 
    42.          $this->Page($each_disNums, $nums, $current_page, $sub_pages, $subPage_link); 
    43.      } 
    44.  */
    45.     /* 
    46.       __destruct析构函数,当类不在使用的时候调用,该函数用来释放资源。 
    47.      */
    48.      function __destruct() {
    49.          unset ($each_disNums);
    50.          unset ($nums);
    51.          unset ($current_page);
    52.          unset ($sub_pages);
    53.          unset ($pageNums);
    54.          unset ($page_array);
    55.          unset ($subPage_link);
    56.      }
    57.    
    58.     /* 
    59.       用来给建立分页的数组初始化的函数。 
    60.      */
    61.      function initArray() {
    62.          for ($= 0; $< $this->sub_pages; $i++) {
    63.              $this->page_array[$i] = $i;
    64.          }
    65.          return $this->page_array;
    66.      }
    67.    
    68.     /* 
    69.       construct_num_Page该函数使用来构造显示的条目 
    70.       即使:[1][2][3][4][5][6][7][8][9][10] 
    71.      */
    72.      function construct_num_Page() {
    73.          if ($this->pageNums < $this->sub_pages) {
    74.              $current_array = array ();
    75.              for ($= 0; $< $this->pageNums; $i++) {
    76.                  $current_array[$i] = $+1;
    77.              }
    78.          } else {
    79.              $current_array = $this->initArray();
    80.              if ($this->current_page <= 3) {
    81.                  for ($= 0; $< count($current_array); $i++) {
    82.                      $current_array[$i] = $+1;
    83.                  }
    84.              }
    85.              elseif ($this->current_page <= $this->pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1) {
    86.                  for ($= 0; $< count($current_array); $i++) {
    87.                      $current_array[$i] = ($this->pageNums) - ($this->sub_pages) + 1 + $i;
    88.                  }
    89.              } else {
    90.                  for ($= 0; $< count($current_array); $i++) {
    91.                      $current_array[$i] = $this->current_page - 2 + $i;
    92.                  }
    93.              }
    94.          }
    95.    
    96.         return $current_array;
    97.      }
    98.    
    99.     /* 
    100.      构造普通模式的分页 
    101.      共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页] 
    102.      */
    103.      function subPageCss1() {
    104.          $subPageCss1Str = "";
    105.          $subPageCss1Str .= "共" . $this->nums . "条记录,";
    106.          $subPageCss1Str .= "每页显示" . $this->each_disNums . "条,";
    107.          $subPageCss1Str .= "当前第" . $this->current_page . "/" . $this->pageNums . "页 ";
    108.          if ($this->current_page > 1) {
    109.              http://blog.csdn.net/phpfenghuo/article/details/$firstPageUrl = $this->subPage_link . "1";
    110.              http://blog.csdn.net/phpfenghuo/article/details/$prewPageUrl = $this->subPage_link . ($this->current_page - 1);
    111.              $subPageCss1Str .= "[首页] ";
    112.              $subPageCss1Str .= "[上一页] ";
    113.          } else {
    114.              $subPageCss1Str .= "[首页] ";
    115.              $subPageCss1Str .= "[上一页] ";
    116.          }
    117.    
    118.         if ($this->current_page < $this->pageNums) {
    119.              http://blog.csdn.net/phpfenghuo/article/details/$lastPageUrl = $this->subPage_link . $this->pageNums;
    120.              http://blog.csdn.net/phpfenghuo/article/details/$nextPageUrl = $this->subPage_link . ($this->current_page + 1);
    121.              $subPageCss1Str .= " [下一页] ";
    122.              $subPageCss1Str .= "[尾页] ";
    123.          } else {
    124.              $subPageCss1Str .= "[下一页] ";
    125.              $subPageCss1Str .= "[尾页] ";
    126.          }
    127.    
    128.         return $subPageCss1Str;
    129.    
    130.     }
    131.    
    132.     /* 
    133.      构造经典模式的分页 
    134.      当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页] 
    135.      */
    136.      function subPageCss2() {
    137.          $subPageCss2Str = "";
    138.          $subPageCss2Str .= "当前第" . $this->current_page . "/" . $this->pageNums . "页 ";
    139.    
    140.         if ($this->current_page > 1) {
    141.              http://blog.csdn.net/phpfenghuo/article/details/$firstPageUrl = $this->subPage_link . "1";
    142.              http://blog.csdn.net/phpfenghuo/article/details/$prewPageUrl = $this->subPage_link . ($this->current_page - 1);
    143.              $subPageCss2Str .= "[首页] ";
    144.              $subPageCss2Str .= "[上一页] ";
    145.          } else {
    146.              $subPageCss2Str .= "[首页] ";
    147.              $subPageCss2Str .= "[上一页] ";
    148.          }
    149.    
    150.         $= $this->construct_num_Page();
    151.          for ($= 0; $< count($a); $i++) {
    152.              $= $a[$i];
    153.              if ($== $this->current_page) {
    154.                  $subPageCss2Str .= "[" . $. "]";
    155.              } else {
    156.                  http://blog.csdn.net/phpfenghuo/article/details/$url = $this->subPage_link . $s;
    157.                  $subPageCss2Str .= "[" . $. "]";
    158.              }
    159.          }
    160.    
    161.         if ($this->current_page < $this->pageNums) {
    162.              http://blog.csdn.net/phpfenghuo/article/details/$lastPageUrl = $this->subPage_link . $this->pageNums;
    163.              http://blog.csdn.net/phpfenghuo/article/details/$nextPageUrl = $this->subPage_link . ($this->current_page + 1);
    164.              $subPageCss2Str .= " [下一页] ";
    165.              $subPageCss2Str .= "[尾页] ";
    166.          } else {
    167.              $subPageCss2Str .= "[下一页] ";
    168.              $subPageCss2Str .= "[尾页] ";
    169.          }
    170.          return $subPageCss2Str;
    171.      }
    172.  }
    173.    
    174.   
    175. //测试一下,看看两种不同效果
    176.   
    177. $current_page=isset($_GET['page'])?intval($_GET['page']):1;//获取用户GET提交的page,如果没有就默
    178.  $= new Page(10, 100, $current_page, 5);
    179.  echo $t->subPageCss2();
    180.  echo "
      "
      ;
    181.  echo $t->subPageCss1();
    182.  ?>

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-10-314-1.html
    相关热词搜索: 分页类 php分页