PHP简单漂亮的分页类

本文介绍一款原生的PHP分页类,分页样式有点类似bootstrap。

  1. <?php
  2.  
  3. /* * *********************************************
  4.  * @类名:   page
  5.  * @参数:   $myde_total - 总记录数
  6.  *          $myde_size - 一页显示的记录数
  7.  *          $myde_page - 当前页
  8.  *          $myde_url - 获取当前的url
  9.  * @功能:   分页实现
  10.  * @作者:   叶国方
  11.  */
  12.  
  13. class page {
  14.  
  15.     private $myde_total;          //总记录数
  16.     private $myde_size;           //一页显示的记录数
  17.     private $myde_page;           //当前页
  18.     private $myde_page_count;     //总页数
  19.     private $myde_i;              //起头页数
  20.     private $myde_en;             //结尾页数
  21.     private $myde_url;            //获取当前的url
  22.     /*
  23.      * $show_pages
  24.      * 页面显示的格式,显示链接的页数为2*$show_pages+1。
  25.      * 如$show_pages=2那么页面上显示就是[首页] [上页] 1 2 3 4 5 [下页] [尾页] 
  26.      */
  27.     private $show_pages;
  28.  
  29.     public function __construct($myde_total = 1, $myde_size = 1, $myde_page = 1, $myde_url, $show_pages = 2) {
  30.         $this->myde_total = $this->numeric($myde_total);
  31.         $this->myde_size = $this->numeric($myde_size);
  32.         $this->myde_page = $this->numeric($myde_page);
  33.         $this->myde_page_count = ceil($this->myde_total / $this->myde_size);
  34.         $this->myde_url = $myde_url;
  35.         if ($this->myde_total < 0)
  36.             $this->myde_total = 0;
  37.         if ($this->myde_page < 1)
  38.             $this->myde_page = 1;
  39.         if ($this->myde_page_count < 1)
  40.             $this->myde_page_count = 1;
  41.         if ($this->myde_page > $this->myde_page_count)
  42.             $this->myde_page = $this->myde_page_count;
  43.         $this->limit = ($this->myde_page - 1) * $this->myde_size;
  44.         $this->myde_i = $this->myde_page - $show_pages;
  45.         $this->myde_en = $this->myde_page + $show_pages;
  46.         if ($this->myde_i < 1) {
  47.             $this->myde_en = $this->myde_en + (1 - $this->myde_i);
  48.             $this->myde_i = 1;
  49.         }
  50.         if ($this->myde_en > $this->myde_page_count) {
  51.             $this->myde_i = $this->myde_i - ($this->myde_en - $this->myde_page_count);
  52.             $this->myde_en = $this->myde_page_count;
  53.         }
  54.         if ($this->myde_i < 1)
  55.             $this->myde_i = 1;
  56.     }
  57.  
  58.     //检测是否为数字
  59.     private function numeric($num) {
  60.         if (strlen($num)) {
  61.             if (!preg_match("/^[0-9]+$/", $num)) {
  62.                 $num = 1;
  63.             } else {
  64.                 $num = substr($num, 0, 11);
  65.             }
  66.         } else {
  67.             $num = 1;
  68.         }
  69.         return $num;
  70.     }
  71.  
  72.     //地址替换
  73.     private function page_replace($page) {
  74.         return str_replace("{page}", $page, $this->myde_url);
  75.     }
  76.  
  77.     //首页
  78.     private function myde_home() {
  79.         if ($this->myde_page != 1) {
  80.             return "<a href='" . $this->page_replace(1) . "' title='首页'>首页</a>";
  81.         } else {
  82.             return "<p>首页</p>";
  83.         }
  84.     }
  85.  
  86.     //上一页
  87.     private function myde_prev() {
  88.         if ($this->myde_page != 1) {
  89.             return "<a href='" . $this->page_replace($this->myde_page - 1) . "' title='上一页'>上一页</a>";
  90.         } else {
  91.             return "<p>上一页</p>";
  92.         }
  93.     }
  94.  
  95.     //下一页
  96.     private function myde_next() {
  97.         if ($this->myde_page != $this->myde_page_count) {
  98.             return "<a href='" . $this->page_replace($this->myde_page + 1) . "' title='下一页'>下一页</a>";
  99.         } else {
  100.             return"<p>下一页</p>";
  101.         }
  102.     }
  103.  
  104.     //尾页
  105.     private function myde_last() {
  106.         if ($this->myde_page != $this->myde_page_count) {
  107.             return "<a href='" . $this->page_replace($this->myde_page_count) . "' title='尾页'>尾页</a>";
  108.         } else {
  109.             return "<p>尾页</p>";
  110.         }
  111.     }
  112.  
  113.     //输出
  114.     public function myde_write($id = 'page') {
  115.         $str = "<div id=" . $id . ">";
  116.         $str.=$this->myde_home();
  117.         $str.=$this->myde_prev();
  118.         if ($this->myde_i > 1) {
  119.             $str.="<p class='pageEllipsis'>...</p>";
  120.         }
  121.         for ($i = $this->myde_i; $i <= $this->myde_en; $i++) {
  122.             if ($i == $this->myde_page) {
  123.                 $str.="<a href='" . $this->page_replace($i) . "' title='第" . $i . "页' class='cur'>$i</a>";
  124.             } else {
  125.                 $str.="<a href='" . $this->page_replace($i) . "' title='第" . $i . "页'>$i</a>";
  126.             }
  127.         }
  128.         if ($this->myde_en < $this->myde_page_count) {
  129.             $str.="<p class='pageEllipsis'>...</p>";
  130.         }
  131.         $str.=$this->myde_next();
  132.         $str.=$this->myde_last();
  133.         $str.="<p class='pageRemark'>共<b>" . $this->myde_page_count .
  134.                 "</b>页<b>" . $this->myde_total . "</b>条数据</p>";
  135.         $str.="</div>";
  136.         return $str;
  137.     }
  138.  
  139. }
  140.  
  141. ?>

 

posted @ 2017-07-09 18:26  叶国方  阅读(666)  评论(0编辑  收藏  举报