PHP 分页代码

Calchas 发表于 2009, April 3, 5:22 PM

PHP代码
  1. require_once("Pagination.class.php");  
  2. //连接数据库  
  3. $dbconn = mssql_connect($webdbhost,$webdbuser,$webdbpwdor die("服务器连接失败.");  
  4. mssql_select_db($webdbnameor die ("数据库连接失败");  
  5. $numrs = mssql_query($strQueryCountor die("数据查询失败");   
  6. $numrow = mssql_fetch_row($numrs);  
  7. $num = $numrow[0]; //总记录   
  8. $result = mssql_query( $strQuery ) or die ("数据查询失败");  
  9. $p = new Pagination();  
  10. $p->setTotal($num);  
  11. $p->initialize();  
  12. $start = $p->getStartNumber();  
  13. $end = $p->getEndNumber();  
  14. for$i = $start$i < $end$i++ ){  
  15.     //echo "<tr><td>".$results[$i][0]."</td><td>".$results[$i]["3"]."</td><td>".$results[$i][5]."  </td></tr>";  
  16.     echo "<tr><td>".mssql_result($result,$i,"GeneralID")."</td><td>".mssql_result($result,$i,"Title")."</td><td>".mssql_result($result,$i,"InputTime")."  </td></tr>";  
  17.     echo "";  
  18. }  
  19. echo "<tr><td colspan='10'>";  
  20. echo $p->getFirstPage();  
  21. echo $p->getPreviousPage();  
  22. echo $p->getNumberBar();  
  23. echo $p->getNextPage();  
  24. echo $p->getLastPage();  
  25. echo $p->getJumpForm();  
  26. echo "</td></tr>";  
  27. mssql_free_result($result);  
  28. mssql_close($dbconn);  


Pagination.class.php

PHP代码
  1. class Pagination{  
  2. /** 
  3. * 总条目数 
  4. */  
  5. private $total;  
  6. /** 
  7. * 每页显示条目数 
  8. */  
  9. private $pageSize = 20;  
  10. /** 
  11. * 总页数 
  12. */  
  13. private $sumPageNumber;  
  14. /** 
  15. * 当前页数 
  16. */  
  17. private $currentlyPageNumber;  
  18. /** 
  19. * 结果集开始条目数 
  20. */  
  21. private $startNumber;  
  22. /** 
  23. * 结果集结束条目数 
  24. */  
  25. private $endNumber;  
  26. /** 
  27. * 翻页时的标识 
  28. */  
  29. private $pageNumberFlag = "page";  
  30. /** 
  31. * 翻页时需要额外带的参数 
  32. */  
  33. private $linksParameter;  
  34. /** 
  35. * 数字翻页条的数量 
  36. */  
  37. private $numberBar = 6;  
  38. /** 
  39. * 首页翻页标识 
  40. */  
  41. private $firstPageFlag = "[首页]";  
  42. /** 
  43. * 尾页翻页标识 
  44. */  
  45. private $lastPageFlag = "[尾页]";  
  46. /** 
  47. * 上页翻页标识 
  48. */  
  49. private $previousPageFlag = "[上页]";  
  50. /** 
  51. * 下页翻页标识 
  52. */  
  53. private $nextPageFlag = "[下页]";  
  54. /** 
  55. * 初始化,在SET相关参数后执行,执行后才可以调用GET 
  56. * @name         initialize 
  57. * @return         void 
  58. */  
  59. function initialize(){  
  60.      
  61. $this->sumPageNumber = ceil$this->total / $this->pageSize );  
  62. if( 0 == $this->sumPageNumber )$this->sumPageNumber++;  
  63. if (isset($_REQUEST[$this->pageNumberFlag]) && is_numeric($_REQUEST[$this->pageNumberFlag]))  
  64. {  
  65.     $this->currentlyPageNumber = $_REQUEST[$this->pageNumberFlag];  
  66. }  
  67. else  
  68. {  
  69.     $this->currentlyPageNumber = 1;  
  70. }  
  71. if ( ( $this->currentlyPageNumber == "" ) ||  
  72.          ( $this->currentlyPageNumber <= 0 ) ||  
  73.          ( $this->currentlyPageNumber > $this->sumPageNumber )  
  74.         ){  
  75.                 $this->currentlyPageNumber = 1;  
  76.         }  
  77.            
  78. $this->startNumber =  
  79.         $this->currentlyPageNumber * $this->pageSize - $this->pageSize;  
  80. $this->endNumber = $this->currentlyPageNumber * $this->pageSize;  
  81. if$this->total < $this->endNumber ){  
  82.         $this->endNumber = $this->total;  
  83. }  
  84. }  
  85. /** 
  86. * 得到含有连接的首页翻页标识 
  87. * @return         string 
  88. */  
  89. function getFirstPage(){  
  90.     if( 1 == $this->currentlyPageNumber){  
  91.             return $this->firstPageFlag . " \n ";         
  92.     }else{  
  93.             return "<a href=\"?" . $this->pageNumberFlag . "=1"$this->linksParameter ."\">" . $this->firstPageFlag . "</a> \n";  
  94.     }  
  95. }  
  96. /** 
  97. * 得到含有连接的尾页翻页标识 
  98. * @return         string 
  99. */  
  100. function getLastPage(){  
  101.     if$this->currentlyPageNumber != $this->sumPageNumber && 0 != $this->sumPageNumber ){  
  102.              return "<a href=\"?" . $this->pageNumberFlag . "=" . $this->sumPageNumber . $this->linksParameter . "\">" . $this->lastPageFlag . "</a> \n";  
  103.     }else{  
  104.              return $this->lastPageFlag . " \n ";         
  105.     }  
  106. }  
  107. /** 
  108. * 得到含有连接的上页翻页标识 
  109. * @return         string 
  110. */  
  111. function getPreviousPage(){  
  112.     if$this->currentlyPageNumber > 1 ){  
  113.              return "<a href=\"?" . $this->pageNumberFlag . "=" . ( $this->currentlyPageNumber - 1 ) . $this->linksParameter ."\">" . $this->previousPageFlag . "</a> \n";  
  114.      }else{  
  115.              return $this->previousPageFlag . " \n";                   
  116.      }  
  117. }  
  118. /** 
  119. * 得到含有连接的下页翻页标识 
  120. * @return         string 
  121. */  
  122. function getNextPage(){  
  123.     if$this->currentlyPageNumber < $this->sumPageNumber && $this->sumPageNumber != 0){  
  124.              return "<a href=\"?" . $this->pageNumberFlag . "=" . ( $this->currentlyPageNumber + 1 ) . $this->linksParameter ."\">" . $this->nextPageFlag . "</a> \n";  
  125.      }else{  
  126.              return $this->nextPageFlag . " \n";  
  127.      }  
  128. }  
  129. /** 
  130. * 得到翻页的数字条 
  131. * @return         string 
  132. */  
  133. function getNumberBar(){  
  134.     if( 0 == ( $this->numberBar % 2 ) ){  
  135.             $this->numberBar++;  
  136.     }  
  137.     $scope = ( $this->numberBar - 1 )/2;  
  138.     $start = $this->currentlyPageNumber - $scope;  
  139.     $end = $this->currentlyPageNumber + $scope;  
  140.     if$start < 1 ){  
  141.             $start = 1;  
  142.     }  
  143.     if$end > $this->sumPageNumber ){  
  144.             $end = $this->sumPageNumber;  
  145.             if$this->sumPageNumber > $this->numberBar ){  
  146.                     $start = $this->sumPageNumber - $this->numberBar + 1;  
  147.             }  
  148.     }  
  149.     if( 1 == $start && $this->sumPageNumber > $this->numberBar ){  
  150.             $end = $this->numberBar;  
  151.     }  
  152.     if$start == $end ){  
  153.             return "<font color=\"red\">[1]</font>\n";  
  154.     }  
  155.     $nb = "";  
  156.     for$i=$start$i<=$end$i++ ){  
  157.             if$i == $this->currentlyPageNumber ){  
  158.                     $nb .= "<font color=\"red\">[" . $i . "]</font> \n";  
  159.                     continue;  
  160.             }  
  161.             $nb .= "<a href=\"?" . $this->pageNumberFlag . "=" . $i . $this->linksParameter . "\">[" . $i . "]</a> \n";  
  162.     }  
  163.      
  164.     return $nb;  
  165. }  
  166. /** 
  167. * 得到可以跳转的表单框,GET方式传递 
  168. * @return         string 
  169. */  
  170. function getJumpForm(){  
  171.     $jf = "<script language=\"javascript\">\n";  
  172.     $jf .= "function _checkForm(){\n";  
  173.     $jf .= "var a = document.getElementById(\"" . $this->pageNumberFlag . "\").value;\n";  
  174.     $jf .= "if( a > " . $this->sumPageNumber . " || a <1 || a == " . $this->currentlyPageNumber . "  ){\n";  
  175.     $jf .= "return false;}else{\n";  
  176.     $jf .= "window.location.href=\"?" . $this->pageNumberFlag . "=\"+a+\"" . $this->linksParameter . "\";";  
  177.     $jf .= "}\n}\n</script>\n";  
  178.     $jf .= "<input class=\"selectButtonCss\" id=\"" . $this->pageNumberFlag . "\" name=\"" . $this->pageNumberFlag . "\" tpye=\"text\" size=\"3\" value=\"" . $this->currentlyPageNumber . "\">\n";  
  179.     $jf .= "<input type=\"button\" name=\"button\" value=\"GO\" onClick=\"return _checkForm()\"  class=\"ButtonCss\">\n";  
  180.      
  181.     return $jf;  
  182. }  
  183. /** 
  184. * 得到结果集的开始数 
  185. * @return         int 
  186. */  
  187. function getStartNumber(){  
  188.     return $this->startNumber;  
  189. }  
  190. /** 
  191. * 得到结果集的结束数 
  192. * @return         int 
  193. */  
  194. function getEndNumber(){  
  195.     return $this->endNumber;  
  196. }  
  197. /** 
  198. * 得到当前页数 
  199. * @return         string 
  200. */  
  201. function getCurrentlyPageNumber(){  
  202.     return $this->currentlyPageNumber;  
  203. }  
  204. /** 
  205. * 得到总页数 
  206. * @return         string 
  207. */  
  208. function getSumPageNumber(){  
  209.     return $this->sumPageNumber;  
  210. }  
  211. /** 
  212. * 得到翻页时需要的所有参数 
  213. * @return         string 
  214. */  
  215. function getAllLinksParameter(){  
  216.     $page = emptyempty($_GET[$this->pageNumberFlag])?0:$_GET[$this->pageNumberFlag];  
  217.     return $this->pageNumberFlag."=".$page.$this->linksParameter;  
  218. }  
  219. /** 
  220. * 设定总的结果集条目数 
  221. * @return         void 
  222. */  
  223. function setTotal( $total ){  
  224.     $this->total = $total;  
  225. }  
  226. /** 
  227. * 设定每页需要显示的结果集条目数 
  228. * @return         void 
  229. */  
  230. function setPageSize( $pageSize ){  
  231.     $this->pageSize = $pageSize;  
  232. }  
  233. /** 
  234. * 设定翻页时传递页数的变量名称 
  235. * @return         void 
  236. */  
  237. function setPageNumberFlag( $pageNumberFlag ){  
  238.     $this->pageNumberFlag = $pageNumberFlag;  
  239. }  
  240. /** 
  241. * 设定翻页时需要额外传递的变量 
  242. * @return         void 
  243. */  
  244. function setLinksParameter( $linksParameter ){  
  245.     $this->linksParameter = $linksParameter;  
  246. }  
  247. /** 
  248. * 设定首页翻页标识 
  249. * @return         void 
  250. */  
  251. function setFirstPageFlag( $firstPageFlag ){  
  252.     $this->firstPageFlag = $firstPageFlag;  
  253. }  
  254. /** 
  255. * 设定尾页翻页标识 
  256. * @return         void 
  257. */  
  258. function setLastPageFlag( $lastPageFlag ){  
  259.     $this->lastPageFlag = $lastPageFlag;  
  260. }  
  261. /** 
  262. * 设定上页翻页标识 
  263. * @return         void 
  264. */  
  265. function setPreviousPageFlag( $previousPageFlag ){  
  266.     $this->previousPageFlag = $previousPageFlag;  
  267. }  
  268. /** 
  269. * 设定下页翻页标识 
  270. * @return         void 
  271. */  
  272. function setNextPageFlag( $nextPageFlag ){  
  273.     $this->nextPageFlag = $nextPageFlag;  
  274. }  
  275. /** 
  276. * 设定翻页数字条所显示的数量 
  277. * @return         void 
  278. */  
  279. function setNumberBar( $numberBar ){  
  280.     $this->numberBar = $numberBar;  
  281. }  
  282. function __set($name,$val){  
  283.     echo "不能在Pagination.class.php中SET 属性 <font color=\"red\">" .$name"</font>";  
  284. }  
  285. function __get($name){  
  286.     echo "不能在Pagination.class.php中GET 属性 <font color=\"red\">" .$name"</font>";  
  287. }  
  288. function __call($name,$arguments){  
  289.     echo "Pagination.class.php中不存在 function <font color=\"red\">" .$name"</font>";  
  290. }  
  291. }  

« 上一篇 | 下一篇 »

标签: php
相关文章: (最多只显示5条记)
PHP读MSSQL日期问题 (浏览:4428, 评论:1)
Undefined index 错误 (浏览:3917, 评论:0)
php5 无法连接 mssql2005 (浏览:4771, 评论:1)
引用: 点击获得Trackback地址,Encode: UTF-8 点击获得Trackback地址,Encode: GB2312 or GBK 点击获得Trackback地址,Encode: BIG5
发表评论:( 你的参与是我最大的动力! )