一个好用的php分页类

原创
2016-07-25 09:02:46 607浏览
  1. /**
  2. 对查询进行分页的类
  3. @link http://bbs.it-home.org
  4. */
  5. class paging
  6. {
  7. private $pageSize; //没一页显示的条数 默认是10条。
  8. private $totlePage; //总共有多少条记录
  9. private $dbConnection;//数据库连接
  10. private $nowPageIndex;//当前显示的页数
  11. private $show; //使用那种方式显示导航,默认的方式是使用show1()首页|上一页|下一页|末页的方式。
  12. /**
  13. 构造函数,建立数据库的连接
  14. @$pageSizeP 没一页显示的条数默认是10条。
  15. @$show 使用那种方式显示导航,默认的方式是使用show1()首页|上一页|下一页|末页的方式。
  16. */
  17. public function _construct($pageSizeP=10,$show="show1")
  18. {
  19. $this->dbConnection = @mysql_connect("localhost","username","password");
  20. if($this->dbConnection)
  21. {
  22. die("");
  23. }
  24. mysql_select_db($this->dbConnection,"databaseName");
  25. $this->show = $show;
  26. $this->pageSize = $pageSizeP;
  27. }
  28. /**
  29. 析构函数,关闭数据库的连接。
  30. */
  31. public function _destruct()
  32. {
  33. @mysql_close($this->dbConnection);
  34. }
  35. /**
  36. 查询数据库,显示数据库的记录条数。
  37. @$sql 查询数据库的sql语句。
  38. @$charset 查询数据库使用的字符集,默认的是UTF-8。
  39. @return 返回数据库查询的结果,保存成数组,然后返回,条数不确定。
  40. */
  41. public function querySQL($sql,$charset="UTF-8")
  42. {
  43. mysql_query("SET NAMES ".$charset);
  44. $rs = @mysql_query($sql);
  45. if(!$rs)
  46. {
  47. die("");
  48. }
  49. $num = @mysql_num_rows($rs);
  50. $this->totlePage= ceil($num/$this->pageSize);
  51. $this->nowPageIndex = (isset($_POST['page']) || $_POST['page'] >= 1):$_POST['page']?1;
  52. if($this->nowPageIndex >$this->totlePage)
  53. {
  54. $this->nowPageIndex = $this->totlePage;
  55. }
  56. $start = ($this->nowPageIndex - 1)*$this->pageSize;
  57. mysql_free_result($rs);
  58. $sql .= "LIMIT $start,$this->pageSize";
  59. $rs = @mysql_query($sql);
  60. if(!$rs)
  61. {
  62. die("");
  63. }
  64. $rows = array();
  65. while($row = @mysql_fetch_row($rs))
  66. {
  67. $rows[] = $row;
  68. }
  69. @mysql_free_result($rs);
  70. return $rows;
  71. }
  72. /**
  73. 显示导航兰。
  74. @$arg 调用显示导航的函数的参数。
  75. $img1 一个数组,保存导航的连接的图片。在调用show1()使用的。
  76. $size 导航兰的一行显示的页数。在调用show2()使用的。
  77. */
  78. public function show($arg)
  79. {
  80. $func = $this->show;
  81. $this->$func($arg);
  82. }
  83. /**
  84. 以首页|上一页|下一页|末页的方式显示导航。
  85. @$img1 首页|上一页|下一页|末页对应的图片路径数组,默认是NULL,既不显示 图片。
  86. */
  87. private function show1($img1 = NULL)
  88. {
  89. $url = $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];
  90. $str = "
  91. 当前$this->nowPageIndex页/共$this->totlePage页";
  92. if(isset($img) || $img != NULL)
  93. {
  94. $str .= "首页
  95. $str .= ">上一页 $str .= ">下一页 $str .= ">末页
    ";
  96. }
  97. else
  98. {
  99. $str .= "首页>
  100. $str .= ">上一页 $str .= ">下一页 $str .= ">末页
    ";
  101. }
  102. echo $str;
  103. }
  104. /**
  105. 以1|2|3|。。。的方式显示导航。
  106. @$size 导航兰每一行显示的页数,默认是10。
  107. */
  108. private function show2($size =10)
  109. {
  110. $url = $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];
  111. $str = "
  112. ";
  113. for($index = 1 ; $index <= $this->totlePage ; $index++)
  114. {
  115. $str .= "
  116. ";
  117. if($index == $size)
  118. {
  119. $str .="
  120. ";
  121. }
  122. }
  123. $str .= "
  124. $str .= "$index
    ";
  125. echo $str;
  126. }
  127. }
  128. ?>
复制代码


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
上一条:我的编程致富之路 下一条:面对新语言和跳槽,程序员如何保持技术优势?

相关文章

查看更多