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

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

分页演示地址:http://www.sucaihuo.com/php/223.html

PHP简单漂亮的分页类的更多相关文章

  1. THinkPHP简单漂亮的分页类 DownLoad

    PHP include_once("config.php"); require_once('page.class.php'); //分页类 $showrow = 10; //一页显 ...

  2. 一个简单的php分页类代码(转载)

    入门级php分页类 原文地址:http://www.xfcodes.com/php/fenye/3608.htm 时间:2015-12-16 20:52:00来源:网络 php分页类. 复制代码代码如 ...

  3. 简单实用的分页类-python

    django自带的分页虽然挺好,但是就想自己弄个通用的 自己写了个分页的类,用的是python,   其他语言改下语法就能用了. #定义好类.class pagemanage: def __init_ ...

  4. php漂亮的分页类

    <?php    /*    * PHP分页类    * @package Page    * @Created 2013-03-27    * @Modify  2013-03-27    * ...

  5. 一个简单的CI分页类

    [php] view plaincopy <span style="font-size:16px;">/** * * 关于 页码有效性的判断需要加在 控制器中判断,即当 ...

  6. 简单实用的原生PHP分页类

    一款简单实用的原生PHP分页类,分页按钮样式简洁美观,页码多的时候显示“...”,也是挺多网站用的效果 核心分页代码 include_once("config.php"); req ...

  7. php 简单分页类

    /**  file: page.class.php   完美分页类 Page  */ class Page {  private $total;          //数据表中总记录数  privat ...

  8. 好用的ASP.NET 分页类 简单好用 支持 AJAX 自定义文字

    在做网站没用 JS UI控件时 很实用 用法: var ps=new PageString(); /*可选参数*/ ps.SetIsEnglish = true;// 是否是英文 (默认:false) ...

  9. 用PHP写的一个简单的分页类 1.0版

    <?php /* 分页类 用于实现对多条数据分页显示 version:1.0 author:Knight E-Mail:S.Knight.Work@gmail.com Date:2013-10- ...

随机推荐

  1. 【BZOJ-4590】自动刷题机 二分 + 判定

    4590: [Shoi2015]自动刷题机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 156  Solved: 63[Submit][Status ...

  2. Unity3d5.0 新UI之2048

    因为汽车系统没写出来所以,纠结之中,弄了弄新版本的UI. 做了个2048. 新版本的unity的UI必须以Canvas为基底来呈现,如果没有加画布的话可是显示不出来东西的哦. 而且作为UI上的所有组件 ...

  3. 树状数组求第k小的元素

    int find_kth(int k) { int ans = 0,cnt = 0; for (int i = 20;i >= 0;i--) //这里的20适当的取值,与MAX_VAL有关,一般 ...

  4. VS快捷键大全(总结了一些记忆的口诀)

    相信.Net开发人员都想能够熟记各种VS快捷键以提高平时开发的效率,但苦于记忆能力太差而快捷键又特别多,特别烦,所以作罢! 下面我将简单介绍一下我记忆VS快捷键的一些方法,希望对大家有所帮助. 1.窗 ...

  5. zoj 3946 Highway Project(最短路 + 优先队列)

    Highway Project Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward, the emperor of the Marjar ...

  6. 强大的css3

    强大的css3 我们知道,这几年来智能手机的高速发展使得人们使用移动端上网的时间和人数已经超过了PC端.例如在2015年,就中国电商而言,各电商平台在移动端持续发力,移动端购物占比不断攀升,双11期间 ...

  7. php删除指定目录所有文件

    <?php /** * 删除指定文件目录下的所有文件 * @param str $dir 指定文件路径: 如:K:/wamp/www/test * return boole *--------- ...

  8. JavaWeb---总结(十六)JSP指令

    一.JSP指令简介 JSP指令(directive)是为JSP引擎而设计的,它们并不直接产生任何可见输出,而只是告诉引擎如何处理JSP页面中的其余部分. 在JSP 2.0规范中共定义了三个指令: pa ...

  9. wpf comboBox取值问题

    这是获取值后台代码 private void button1_Click(object sender, RoutedEventArgs e)        {            combBox = ...

  10. css009 装饰网站的导航

    css009 装饰网站的导航 1.         选择定义样式的链接 1.连接的状态: A.未访问  a:link{C;} B.已访问  a:visited{ color : red; } C.鼠标 ...