一个分页显示类

  1. <?php
  2. /*
  3. * 分页显示类
  4. * PageItem.php v 1.0.0
  5. * 编程:Boban<boban@21php.com>
  6. * 讨论:http://www.phpfensi.com
  7. * 更新:2004-02-02
  8. * 说明:
  9. * 1. 配合MYSQL数据库使用
  10. * 2. 类没有提供连接数据库的功能,需在外部建立数据库连接。
  11. * */
  12. /*
  13. * 使用方法:
  14. * $sql = "select * from news limit 0,10";
  15. * $hdc = new PageItem($sql);
  16. * echo $hdc->myPageItem();
  17. * $arrRecords = $hdc->ReadList();
  18. * */
  19. if (!defined("__PAGEITEM__")) {
  20. define("__PAGEITEM__", 1);
  21. } else {
  22. exit(3);
  23. }
  24. class PageItem {
  25. var $iDefaultRecords = 10; // 默认每页显示记录数,如果没有设置,就使用默认值
  26. var $iMaxRecord; //每页记录数
  27. var $iTotal; //记录总数
  28. var $sqlRecord; // 获取记录的SQL查询
  29. var $iPages; //总页数
  30. var $CPages; //当前页数
  31. /*
  32. * 构造函数 -- 初始化变量
  33. * 参数:SQL查询语句,将忽略LIMIT语句
  34. * */
  35. function PageItem($sql = "")
  36. {
  37. // register_shutdown_function($this->_PageItem());
  38. $this->SetMaxRecord($this->iDefaultRecords);
  39. /*
  40. * 解析SQL语句
  41. * */
  42. if ($sql <> "") {
  43. list($sql) = spliti("LIMIT", $sql); // 去除LIMIT语句
  44. $this->sqlRecord = trim($sql);
  45. list(, $sql) = spliti("FROM", $sql);
  46. $sql = trim($sql);
  47. if(preg_match ("/\bGROUP\b \bBY\b/i", $sql))
  48. {
  49. //echo "HAVE GROUP BY";
  50. if(preg_match ("/\bHAVING\b/i", $sql)) list(,$field) = spliti("HAVING",$sql);
  51. list($field) = spliti(' ',trim($field));
  52. //echo $field;
  53. $this->iTotal = $this->CountRecord("SELECT $field,COUNT(DISTINCT $field) AS cnt FROM " . $sql,2);
  54. }
  55. else $this->iTotal = $this->CountRecord("SELECT COUNT(*) AS cnt FROM " . $sql,1);
  56. }
  57. $this->iPages = ceil($this->iTotal / $this->iMaxRecord);
  58. $this->CPages = $_REQUEST['page'];
  59. if ($this->CPages <= 0) $this->CPages = 1;
  60. if ($this->CPages > $this->iPages) $this->CPages = $this->iPages;
  61. //echo "SELECT COUNT(*) AS cnt FROM " . $sql;
  62. //echo $this->iTotal;
  63. }
  64. /*
  65. * 析构函数 -- 暂时不可用
  66. * */
  67. function _PageItem()
  68. {
  69. // $this->linkid = NULL;
  70. }
  71. function SetMaxRecord($cnt)
  72. {
  73. $this->iMaxRecord = $cnt;
  74. }
  75. /*
  76. * 统计匹配的记录总数
  77. * */
  78. function CountRecord($sql,$type)
  79. {
  80. //echo $sql;
  81. if($type == 1)
  82. {
  83. if (($records = mysql_query($sql)) && ($record = mysql_fetch_assoc($records))) {
  84. return $record['cnt'];
  85. } else return 0;
  86. }
  87. elseif($type == 2)
  88. {
  89. if($records = mysql_query($sql))
  90. return mysql_affected_rows();
  91. }
  92. }
  93. /*
  94. * 读取记录
  95. * */
  96. function ReadList()
  97. {
  98. $ret = array();
  99. $this->sqlRecord.=" LIMIT ".($this->CPages-1)*$this->iMaxRecord.",".$this->iMaxRecord;
  100. $records = mysql_query($this->sqlRecord);
  101. if(!$records) return;
  102. while($record = mysql_fetch_array($records))
  103. {
  104. $ret[] = $record;
  105. }
  106. return $ret;
  107. }
  108. function LinktoPage($page, $msg)
  109. {
  110. $link = $this->PageUrl($page);
  111. return "<A href=\"$link\">$msg</A>\n";
  112. }
  113. function PageUrl($page)
  114. {
  115. $phpself = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];
  116. $querystring = $_SERVER['QUERY_STRING'];
  117. $querystring = preg_replace("/page=[0-9]*&?/i", "", $querystring);
  118. $link = $phpself . "?page=$page&" . $querystring;
  119. return $link;
  120. }
  121. /*
  122. * 显示当前页及总页数
  123. * */
  124. function PageNav()
  125. {
  126. echo "第" . $this->CPages . "页/共" . $this->iPages . "页";
  127. }
  128. /*
  129. * 显示翻页按钮,包括首页、下页、上页、未页
  130. * */
  131. function PageButton()
  132. {
  133. if ($this->CPages > 1) {
  134. echo $this->LinktoPage(1, "首页");
  135. echo " | ";
  136. echo $this->LinktoPage($this->CPages-1, "上一页");
  137. } else {
  138. echo "首页 | 上一页";
  139. }
  140. if ($this->CPages < $this->iPages) {
  141. echo " | ";
  142. echo $this->LinktoPage($this->CPages + 1, "下一页");
  143. echo " | ";
  144. echo $this->LinktoPage($this->iPages, "首页");
  145. } else {
  146. echo " | 下一页 | 尾页";
  147. }
  148. }
  149. /*
  150. * 显示跳转页选择框
  151. * */
  152. function SelectItem()
  153. {
  154. echo "跳到第<SELECT name='topage' size='1' onchange='window.location=this.value'>\n";
  155. for($i = 1;$i <= $this->iPages;$i++) {
  156. if ($this->CPages == $i)
  157. $extra = "selected";
  158. else
  159. $extra = "";
  160. echo "<OPTION VALUE='" . $this->PageUrl($i) . "' $extra>$i</OPTION>";
  161. }
  162. echo "</SELECT>\n";
  163. }
  164. /*
  165. * 一次性显示所有按钮组件
  166. * */
  167. function myPageItem()
  168. {
  169. $this->PageButton();
  170. $this->SelectItem();
  171. $this->PageNav();
  172. }
  173. } // 类结束
  174. ?>