简单的php分页类

  1. <?php
  2. //-------------------------------------------------------------------------
  3. //使用方法
  4. //测试时还需要一张表 至少出现字段 announceID,announceContent
  5. //include db.php
  6. $db_host='localhost';
  7. $db_user='username';
  8. $db_password='password';
  9. $db_name='konwu';
  10. $db_prefix = 'konwu_';
  11. Global $db_prefix;
  12. $conn=@mysql_connect($db_host,$db_user,$db_password);
  13. // utf-8 是默认设置 即使没有下面的一行也会默认 UTF-8
  14. // 但是MYSQL数据库的默认是LATIN 拉丁字符
  15. // 最好设置成 GBK UTF-8包含GBK 包含 LATIN
  16. //网页用UTF-8 显示 GBK 没关系 但如果 MYSQL写入时用UTF8有乱码风险,因为MYSQL安装时默认LATIN ,最好 SET GBK
  17. mysql_query("SET NAMES 'UTF8'");
  18. mysql_select_db($db_name,$conn) or die ('none DB');
  19. //set page
  20. //@param set
  21. $errorURL = 'http://localhost:8080/htmls/error/index.html';
  22. $result = mysql_query("SELECT * FROM konwu_messageannounce");
  23. $touNUM = mysql_num_rows($result);
  24. $config = array('DB_PREFIX'=>'konwu_','DB'=>'messageannounce','PER_NUM'=>10,'TOU_NUM'=>$touNUM,'ERROR_URL'=>$errorURL,'NUM_PAGE'=>'p');
  25. // use class
  26. $pages = new Pages;
  27. $getPage = $pages->resultNUM($config);//
  28. //------------------------------------
  29. class Pages{
  30. public static $perNUM;//每一页多少条
  31. public static $touNUM;//总共多少条
  32. public static $pageNUM;//总页数
  33. public static $tables;//表名
  34. public static $errorURL;// index URL
  35. public static $numPage;//页面参数的标识 ,
  36. function getInt($config=array()){
  37. $this->tables = $config['DB_PREFIX'].$config['DB'] ? $config['DB_PREFIX'].$config['DB'] : 'konwu_messageannounce';
  38. $this->perNUM = $config['PER_NUM'] ? $config['PER_NUM'] : 10;
  39. $this->touNUM = $config['TOU_NUM'] ? $config['TOU_NUM'] : 50;
  40. $this->pageNUM = ceil($this->touNUM/$this->perNUM);
  41. $this->errorURL = $config['ERROR_URL'] ? $config['ERROR_URL'] : 'http://www.konwu.com';
  42. $this->numPage = $config['NUM_PAGE'] ? $config['NUM_PAGE'] : 'page';
  43. }
  44. function getNUM(){//get num 得到当前页面的值
  45. if(isset($_GET[$this->numPage]) && !emptyempty($_GET[$this->numPage])){
  46. $str = $_GET[$this->numPage];
  47. }else{
  48. $str = 'wrong';
  49. }
  50. return $str;
  51. }
  52. function getError(){
  53. //return $str;
  54. header('location: '.$this->errorURL);
  55. }
  56. //$_GET['pageNUM'] 第几页,必须要是数字整数,必须要 >0 ,必须 <=$pageNUM
  57. function isNUM(){//check num
  58. $str = self::getNUM();
  59. if($str != 'wrong'){
  60. if(ctype_digit($str)){
  61. $numPage = $str;
  62. }else{
  63. self::getError();
  64. }
  65. }else{
  66. self::getError();
  67. }
  68. return $numPage;
  69. }
  70. function getSelect(){
  71. $sql = 'SELECT * FROM '.$this->tables.' ORDER BY announceID DESC';
  72. return $sql;
  73. }
  74. function getSelectResult(){
  75. $sql = self::getSelect();
  76. $result = mysql_query($sql);
  77. return $result;
  78. }
  79. function getStartNUM(){
  80. $nowPage = self::isNUM();
  81. $perNUM = $this->perNUM;
  82. $startNUM = $nowPage-1;
  83. $startNUM = $startNUM*$perNUM;
  84. $startNUM = $startNUM+1;
  85. return $startNUM;
  86. }
  87. function getLastNUM(){
  88. $nowPage = self::isNUM();
  89. $perNUM = $this->perNUM;
  90. $lastNUM = $nowPage*$perNUM;
  91. return $lastNUM;
  92. }
  93. function resultNUM($config){
  94. self::getInt($config);
  95. $result = self::getSelectResult();
  96. $startNUM = self::getStartNUM();
  97. $lastNUM = self::getLastNUM();
  98. $mynum = 1;
  99. $allResult = '';
  100. while($re=mysql_fetch_array($result)){
  101. if($mynum>=$startNUM && $mynum<=$lastNUM){
  102. $announceID = $re['announceID'];
  103. $singleResult = $re['announceContent'];
  104. $singleResult = mb_substr($singleResult,0,50,'utf-8');
  105. $lastPage = self::lastPage();
  106. $nextPage = self::nextPage();
  107. $beginPage = self::beginPage();
  108. $endPage = self::endPage();
  109. $getFivePage = self::getFivePage();
  110. $singleResult = '
  111. <p>'.$singleResult.'&hellip;&nbsp;&nbsp;<a href="moreInfoCon'.$announceID.'.html">详细</a></p>
  112. ';
  113. $allResult = $allResult.$singleResult;
  114. }
  115. //--------------------------------------------------------------
  116. $mynum++;
  117. //----------------------------
  118. if($mynum>$lastNUM){
  119. $resultPage = '<div class="pagination">
  120. '.$beginPage.'
  121. '.$lastPage.'
  122. '.$getFivePage.'
  123. '.$nextPage.'
  124. '.$endPage.'
  125. </div>';
  126. $allResult = $allResult.$resultPage;
  127. return $allResult;
  128. }
  129. //-----------------------------
  130. }
  131. $resultPage = '<div class="pagination">
  132. '.$beginPage.'
  133. '.$lastPage.'
  134. '.$getFivePage.'
  135. '.$nextPage.'
  136. '.$endPage.'
  137. </div>';
  138. $allResult = $allResult.'<br><br>'.$resultPage;
  139. return $allResult;
  140. }
  141. //next page下一页
  142. function nextPage(){
  143. $nowPage = self::isNUM();
  144. $pageNUM = $this->pageNUM;
  145. if($nowPage>=$pageNUM){
  146. $nextPage = '';
  147. }else{
  148. $nextPage = $nowPage+1;
  149. $nextPage = '<a href="moreInfo.html?'.$this->numPage.'='.$nextPage.'" title="Next Page">下一页 &raquo;</a>';
  150. }
  151. return $nextPage;//得到参数值
  152. }
  153. //lastPage 上一页
  154. function lastPage(){
  155. $nowPage = self::isNUM();
  156. if($nowPage>=2){
  157. $lastPage = $nowPage-1;
  158. $lastPage = '<a href="moreInfo.html?'.$this->numPage.'='.$lastPage.'" title="Previous Page">&laquo; 上一页</a>';
  159. }else{
  160. $lastPage = '';
  161. }
  162. return $lastPage;
  163. }
  164. //第一页,最后一页,当前页左右各两页共五页
  165. function endPage(){
  166. $pageNUM = $this->pageNUM;
  167. $endPage = '<a href="moreInfo.html?'.$this->numPage.'='.$pageNUM.'" title="Last Page">最后一页 &raquo;</a>';
  168. return $endPage;
  169. }
  170. function beginPage(){
  171. $beginPage = '<a href="moreInfo.html?'.$this->numPage.'=1" title="First Page">&laquo; 第一页</a>';
  172. return $beginPage;
  173. }
  174. function getFivePage(){
  175. $nowPage = self::isNUM();//当前页面
  176. $pageNUM = $this->pageNUM;//总页数
  177. if($pageNUM<=5){
  178. $NUM = 1;
  179. $getNUM = '';
  180. while($pageNUM>=$NUM){
  181. $nums = '<a href="moreInfo.html?'.$this->numPage.'='.$NUM.'" class="number" title="'.$NUM.'">'.$NUM.'</a>';
  182. $getNUM = $getNUM.$nums;
  183. $NUM++;
  184. }
  185. $getFivePage = $getNUM;
  186. return $getFivePage;
  187. }else{//>5
  188. if($nowPage == 1){
  189. $getNUM = '
  190. <a href="moreInfo.html?'.$this->numPage.'=1" class="number current" title="1">1</a>
  191. <a href="moreInfo.html?'.$this->numPage.'=2" class="number" title="2">2</a>
  192. <a href="moreInfo.html?'.$this->numPage.'=3" class="number" title="3">3</a>
  193. <a href="moreInfo.html?'.$this->numPage.'=4" class="number" title="4">4</a>
  194. <a href="moreInfo.html?'.$this->numPage.'=5" class="number" title="5">5</a>
  195. ';
  196. }elseif($nowPage == 2){
  197. $getNUM = '
  198. <a href="moreInfo.html?'.$this->numPage.'=1" class="number" title="1">1</a>
  199. <a href="moreInfo.html?'.$this->numPage.'=2" class="number current" title="2">2</a>
  200. <a href="moreInfo.html?'.$this->numPage.'=3" class="number" title="3">3</a>
  201. <a href="moreInfo.html?'.$this->numPage.'=4" class="number" title="4">4</a>
  202. <a href="moreInfo.html?'.$this->numPage.'=5" class="number" title="5">5</a>
  203. ';
  204. }elseif($nowPage == ($pageNUM-1)){//-2位置
  205. $getNUM = '
  206. <a href="moreInfo.html?'.$this->numPage.'='.($pageNUM-4).'" class="number" title="'.($pageNUM-4).'">'.($pageNUM-4).'</a>
  207. <a href="moreInfo.html?'.$this->numPage.'='.($pageNUM-3).'" class="number" title="'.($pageNUM-3).'">'.($pageNUM-3).'</a>
  208. <a href="moreInfo.html?'.$this->numPage.'='.($pageNUM-2).'" class="number" title="'.($pageNUM-2).'">'.($pageNUM-2).'</a>
  209. <a href="moreInfo.html?'.$this->numPage.'='.($pageNUM-1).'" class="number current" title="'.($pageNUM-1).'">'.($pageNUM-1).'</a>
  210. <a href="moreInfo.html?'.$this->numPage.'='.$pageNUM.'" class="number" title="'.$pageNUM.'">'.$pageNUM.'</a>
  211. ';
  212. }elseif($nowPage == $pageNUM){//-1位置
  213. $getNUM = '
  214. <a href="moreInfo.html?'.$this->numPage.'='.($nowPage-4).'" class="number" title="'.($nowPage-4).'">'.($nowPage-4).'</a>
  215. <a href="moreInfo.html?'.$this->numPage.'='.($nowPage-3).'" class="number" title="'.($nowPage-3).'">'.($nowPage-3).'</a>
  216. <a href="moreInfo.html?'.$this->numPage.'='.($nowPage-2).'" class="number" title="'.($nowPage-2).'">'.($nowPage-2).'</a>
  217. <a href="moreInfo.html?'.$this->numPage.'='.($nowPage-1).'" class="number" title="'.($nowPage-1).'">'.($nowPage-1).'</a>
  218. <a href="moreInfo.html?'.$this->numPage.'='.$nowPage.'" class="number current" title="'.$nowPage.'">'.$nowPage.'</a>
  219. ';
  220. }elseif(2<$nowPage && $nowPage<($pageNUM-1)){//2位置和-2位置之间
  221. $getNUM = '
  222. <a href="moreInfo.html?'.$this->numPage.'='.($nowPage-2).'" class="number" title="'.($nowPage-2).'">'.($nowPage-2).'</a>
  223. <a href="moreInfo.html?'.$this->numPage.'='.($nowPage-1).'" class="number" title="'.($nowPage-1).'">'.($nowPage-1).'</a>
  224. <a href="moreInfo.html?'.$this->numPage.'='.$nowPage.'" class="number current" title="'.$nowPage.'">'.$nowPage.'</a>
  225. <a href="moreInfo.html?'.$this->numPage.'='.($nowPage+1).'" class="number" title="'.($nowPage+1).'">'.($nowPage+1).'</a>
  226. <a href="moreInfo.html?'.$this->numPage.'='.($nowPage+2).'" class="number" title="'.($nowPage+2).'">'.($nowPage+2).'</a>
  227. ';
  228. }else{
  229. self::getError();
  230. }
  231. }
  232. $getFivePage = $getNUM;
  233. return $getFivePage;
  234. }
  235. //----------------------------------
  236. }
  237. ?>