1. <?php
  2.  
  3. /**
  4. * 页面名称:cls_page.php
  5. */
  6. class Page {
  7. private $each_disNums; //每页显示的条目数
  8. private $nums; //总条目数
  9. private $current_page; //当前被选中的页
  10. private $sub_pages; //每次显示的页数
  11. private $pageNums; //总页数
  12. private $page_array = array (); //用来构造分页的数组
  13. private $subPage_link; //每个分页的链接
  14.  
  15. /**
  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) {
  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. }
  42. /**
  43. * 照顾低版本
  44. */
  45. /*
  46. function __construct($each_disNums, $nums, $current_page, $sub_pages, $subPage_linke) {
  47. $this->Page($each_disNums, $nums, $current_page, $sub_pages, $subPage_link);
  48.  
  49. }
  50. */
  51.  
  52. /*
  53. __destruct析构函数,当类不在使用的时候调用,该函数用来释放资源。
  54. */
  55. function __destruct() {
  56. unset ($each_disNums);
  57. unset ($nums);
  58. unset ($current_page);
  59. unset ($sub_pages);
  60. unset ($pageNums);
  61. unset ($page_array);
  62. unset ($subPage_link);
  63. }
  64.  
  65. /*
  66. 用来给建立分页的数组初始化的函数。
  67. */
  68. function initArray() {
  69. for ($i = 0; $i < $this->sub_pages; $i++) {
  70. $this->page_array[$i] = $i;
  71. }
  72. return $this->page_array;
  73. }
  74.  
  75. /*
  76. construct_num_Page该函数使用来构造显示的条目
  77. 即使:[1][2][3][4][5][6][7][8][9][10]
  78. */
  79. function construct_num_Page() {
  80. if ($this->pageNums < $this->sub_pages) {
  81. $current_array = array ();
  82. for ($i = 0; $i < $this->pageNums; $i++) {
  83. $current_array[$i] = $i +1;
  84. }
  85. } else {
  86. $current_array = $this->initArray();
  87. if ($this->current_page <= 3) {
  88. for ($i = 0; $i < count($current_array); $i++) {
  89. $current_array[$i] = $i +1;
  90. }
  91. }
  92. elseif ($this->current_page <= $this->pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1) {
  93. for ($i = 0; $i < count($current_array); $i++) {
  94. $current_array[$i] = ($this->pageNums) - ($this->sub_pages) + 1 + $i;
  95. }
  96. } else {
  97. for ($i = 0; $i < count($current_array); $i++) {
  98. $current_array[$i] = $this->current_page - 2 + $i;
  99. }
  100. }
  101. }
  102.  
  103. return $current_array;
  104. }
  105.  
  106. /*
  107. 构造普通模式的分页
  108. 共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页]
  109. */
  110. function subPageCss1() {
  111. $subPageCss1Str = "";
  112. $subPageCss1Str .= "共" . $this->nums . "条记录,";
  113. $subPageCss1Str .= "每页显示" . $this->each_disNums . "条,";
  114. $subPageCss1Str .= "当前第" . $this->current_page . "/" . $this->pageNums . "页 ";
  115. if ($this->current_page > 1) {
  116. $firstPageUrl = $this->subPage_link . "1";
  117. $prewPageUrl = $this->subPage_link . ($this->current_page - 1);
  118. $subPageCss1Str .= "[<a href='$firstPageUrl'>首页</a>] ";
  119. $subPageCss1Str .= "[<a href='$prewPageUrl'>上一页</a>] ";
  120. } else {
  121. $subPageCss1Str .= "[首页] ";
  122. $subPageCss1Str .= "[上一页] ";
  123. }
  124.  
  125. if ($this->current_page < $this->pageNums) {
  126. $lastPageUrl = $this->subPage_link . $this->pageNums;
  127. $nextPageUrl = $this->subPage_link . ($this->current_page + 1);
  128. $subPageCss1Str .= " [<a href='$nextPageUrl'>下一页</a>] ";
  129. $subPageCss1Str .= "[<a href='$lastPageUrl'>尾页</a>] ";
  130. } else {
  131. $subPageCss1Str .= "[下一页] ";
  132. $subPageCss1Str .= "[尾页] ";
  133. }
  134.  
  135. return $subPageCss1Str;
  136.  
  137. }
  138.  
  139. /*
  140. 构造经典模式的分页
  141. 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
  142. */
  143. function subPageCss2() {
  144. $subPageCss2Str = "";
  145. $subPageCss2Str .= "当前第" . $this->current_page . "/" . $this->pageNums . "页 ";
  146.  
  147. if ($this->current_page > 1) {
  148. $firstPageUrl = $this->subPage_link . "1";
  149. $prewPageUrl = $this->subPage_link . ($this->current_page - 1);
  150. $subPageCss2Str .= "[<a href='$firstPageUrl'>首页</a>] ";
  151. $subPageCss2Str .= "[<a href='$prewPageUrl'>上一页</a>] ";
  152. } else {
  153. $subPageCss2Str .= "[首页] ";
  154. $subPageCss2Str .= "[上一页] ";
  155. }
  156.  
  157. $a = $this->construct_num_Page();
  158. for ($i = 0; $i < count($a); $i++) {
  159. $s = $a[$i];
  160. if ($s == $this->current_page) {
  161. $subPageCss2Str .= "[<span style='color:red;font-weight:bold;'>" . $s . "</span>]";
  162. } else {
  163. $url = $this->subPage_link . $s;
  164. $subPageCss2Str .= "[<a href='$url'>" . $s . "</a>]";
  165. }
  166. }
  167.  
  168. if ($this->current_page < $this->pageNums) {
  169. $lastPageUrl = $this->subPage_link . $this->pageNums;
  170. $nextPageUrl = $this->subPage_link . ($this->current_page + 1);
  171. $subPageCss2Str .= " [<a href='$nextPageUrl'>下一页</a>] ";
  172. $subPageCss2Str .= "[<a href='$lastPageUrl'>尾页</a>] ";
  173. } else {
  174. $subPageCss2Str .= "[下一页] ";
  175. $subPageCss2Str .= "[尾页] ";
  176. }
  177. return $subPageCss2Str;
  178. }
  179. }
  180.  
  181. ?>
  182.  
  183. <?
  184. class sqldao{
  185. //连接数据库
  186. function conn(){
  187. $dbh=mysql_connect('localhost','username','pass')or die("对不起,数据库连接错误!请稍候再来,或与管理员联系");
  188. mysql_query("set names 'utf8'", $dbh);
  189. mysql_select_db('tableName');
  190. return $dbh;
  191. }
  192. }
  193. ?>
  194.  
  195. <?
  196. //以下为测试代码
  197. $sqldao=new sqldao();
  198. $dbh=$sqldao->conn();
  199.  
  200. $start=($_GET['p']-1)*10;
  201. $que="select utname from renyuan";
  202. $rs=mysql_query($que,$dbh);
  203. $num_all=mysql_num_rows($rs);
  204.  
  205. $rs=mysql_query($que." limit ".$start.",10",$dbh);
  206. $num=mysql_num_rows($rs);
  207. for($i=0;$i<$num;$i++){
  208. $row=mysql_fetch_array($rs);
  209. echo $row['utname']."<br>";
  210. }
  211.  
  212. echo "<hr>";
  213. //测试一下,看看两种不同效果
  214. //$t = new Page(显示条数, 数据总数, 页数, 页数链接个数, '页面链接');
  215. $t = new Page(10, $num_all, $_GET['p'], 10, 'cls_page.php?p=');
  216. echo $t->subPageCss2();
  217. echo "<p>";
  218. echo $t->subPageCss1();
  219.  
  220. ?>

php动态分页类的更多相关文章

  1. DedeCMS织梦动态分页类,datalist标签使用实例

    <?php require_once(dirname(__FILE__)."/include/common.inc.php");//载入基础文件 require_once(D ...

  2. Java 动态分页类

     动态分页类: Cls_page.java package pagination; public class Cls_page { private int nums;// 总条目数 private i ...

  3. PHP+jQuery 长文章分页类 ( 支持 url / ajax 分页方式 )

    /* ******* 环境:Apache2.2.8 ( 2.2.17 ) + PHP5.2.6 ( 5.3.3 ) + MySQL5.0.51b ( 5.5.8 ) + jQuery-1.8 **** ...

  4. LayUI分页,LayUI动态分页,LayUI laypage分页,LayUI laypage刷新当前页

    LayUI分页,LayUI动态分页,LayUI laypage分页,LayUI laypage刷新当前页 >>>>>>>>>>>> ...

  5. Tornado-基于正则的路由和动态分页

    概览 这一小节涉及了三部分内容: 1.动态分页设计 2.基本的路由系统以及基于正则的路由 3.模块引擎的继承和导入 4.web项目文件夹和ReuquestHandler的分类 5.跨站脚本攻击 文件结 ...

  6. Java动态生成类以及动态添加属性

    有个技术实现需求:动态生成类,其中类中的属性来自参数对象中的全部属性以及来自参数对象properties文件. 那么技术实现支持:使用CGLib代理. 具体的实现步骤: 1.配置Maven文件: &l ...

  7. customPage.class.php可添加js事件的分页类

    用于ajax动态加载数据的分页类,分页事件可以动态添加,去除了a链接中的href地址. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...

  8. jquery动态分页

    最近一直研究jquery的分页效果,刚刚弄好了一个,拿出来与大家分享.分页效果与时光网的差不多. 网址:http://www.mtime.com/movie/news/all/ 先在aspx页面放置一 ...

  9. php实现的分页类

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

随机推荐

  1. 移动端touchstar、touchmove、touchend 事件如果页面有滚动时不让触发 touchend 事件。

    /*仅适用于内容中点击元素.对于拖动等元素,需要自行在页面处理. * 主要是绑定touchstart和touchmove事件,并判断用户按下之后手指移动了多少像素. * 如果手指移动距离小于10像素, ...

  2. 查看Jquery版本

    1. $.fn.jquery > "1.11.1" 2. 通过这样可以 判断一个对象是否是jquery对象!!

  3. hdoj 1176(可转化为数塔)

    免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submissio ...

  4. C++中 _itoa_s方法简介

    _itoa_s 函数原型如下: _itoa_s ( int value, char *buffer, size_t sizeInCharacters, //存放结果的字符数组长度 int radix ...

  5. 了解负载均衡 会话保持 session同步(转)

    一,什么负载均衡 一个新网站是不要做负载均衡的,因为访问量不大,流量也不大,所以没有必要搞这些东西.但是随着网站访问量和流量的快速增长,单台服务器受自身硬件条件的限制,很难承受这么大的访问量.在这种情 ...

  6. Ubuntu Apache 伪静态配置 url重写 步骤

    1.加载rewrite模块sudo ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.l ...

  7. DB2JAVIT:RC=9505解决方案

    DB2JAVIT:RC=9505解决方案 题记:WIN7下装DB2,启动任务中心.控制中心报DB2JAVIT:RC=9505. 解决方案:进入(计算机—>管理—>本地用户和组)把用户加入到 ...

  8. Python自动化运维之22、JavaScript

    一.简介 JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理.学习了htm ...

  9. Unity问答——NGUI怎么使用按键模拟鼠标点击?

    这篇博客源自我在泰课在线的回答.链接:http://www.taikr.com/group/1/thread/248 问:NGUI怎么模拟用代码模拟控制点击 答: 1. 这个问题问得好.因为在使用按键 ...

  10. [BZOJ 2243] [SDOI 2011] 染色 【树链剖分】

    题目链接:BZOJ - 2243 题目分析 树链剖分...写了200+行...Debug了整整一天+... 静态读代码读了 5 遍 ,没发现错误,自己做小数据也过了. 提交之后全 WA . ————— ...