完美php文章分页类

  1. class SubPages{
  2. private $each_disNums;//每页显示的条目数
  3. private $nums;//总条目数
  4. private $current_page;//当前被选中的页
  5. private $sub_pages;//每次显示的页数
  6. private $pageNums;//总页数
  7. private $page_array = array();//用来构造分页的数组
  8. private $subPage_link;//每个分页的链接
  9. private $subPage_type;//显示分页的类型
  10. /*
  11. __construct是SubPages的构造函数,用来在创建类的时候自动运行.
  12. @$each_disNums 每页显示的条目数
  13. @nums 总条目数
  14. @current_num 当前被选中的页
  15. @sub_pages 每次显示的页数
  16. @subPage_link 每个分页的链接
  17. @subPage_type 显示分页的类型
  18. 当@subPage_type=1的时候为普通分页模式
  19. example: 共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页]
  20. 当@subPage_type=2的时候为经典分页样式
  21. example: 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
  22. */
  23. function __construct($each_disNums,$nums,$current_page,$sub_pages,$subPage_link,$subPage_type){
  24. $this->each_disNums=intval($each_disNums);
  25. $this->nums=intval($nums);
  26. if(!$current_page){
  27. $this->current_page=1;
  28. }else{
  29. $this->current_page=intval($current_page);
  30. }
  31. $this->sub_pages=intval($sub_pages);
  32. $this->pageNums=ceil($nums/$each_disNums);
  33. $this->subPage_link=$subPage_link;
  34. $this->show_SubPages($subPage_type); //调用show_SubPages函数
  35. //echo $this->pageNums."--".$this->sub_pages;
  36. }
  37. function __destruct(){
  38. unset($each_disNums);
  39. unset($nums);
  40. unset($current_page);
  41. unset($sub_pages);
  42. unset($pageNums);
  43. unset($page_array);
  44. unset($subPage_link);
  45. unset($subPage_type);
  46. }
  47. /*
  48. show_SubPages函数用在构造函数里面。而且用来判断显示什么样子的分页
  49. */
  50. function show_SubPages($subPage_type){
  51. if($subPage_type == 1){
  52. $this->subPageCss1();
  53. }elseif ($subPage_type == 2){
  54. $this->subPageCss2();
  55. }
  56. }
  57. /*
  58. 用来给建立分页的数组初始化的函数。
  59. */
  60. function initArray(){
  61. for($i=0;$i<$this->sub_pages;$i++){
  62. $this->page_array[$i]=$i;
  63. }
  64. return $this->page_array;
  65. }
  66. /*
  67. construct_num_Page该函数使用来构造显示的条目
  68. 即使:[1][2][3][4][5][6][7][8][9][10]
  69. */
  70. function construct_num_Page(){
  71. if($this->pageNums < $this->sub_pages){
  72. $current_array=array();
  73. for($i=0;$i<$this->pageNums;$i++){
  74. $current_array[$i]=$i+1;
  75. }
  76. }else{
  77. $current_array=$this->initArray();
  78. if($this->current_page <= 3){
  79. for($i=0;$i<count($current_array);$i++){
  80. $current_array[$i]=$i+1;
  81. }
  82. }elseif ($this->current_page <= $this->pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1 ){
  83. for($i=0;$i<count($current_array);$i++){
  84. $current_array[$i]=($this->pageNums)-($this->sub_pages)+1+$i;
  85. }
  86. }else{
  87. for($i=0;$i<count($current_array);$i++){
  88. $current_array[$i]=$this->current_page-2+$i;
  89. }
  90. }
  91. }
  92. return $current_array;
  93. }
  94. /*
  95. 构造普通模式的分页
  96. 共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页]
  97. */
  98. function subPageCss1(){
  99. $subPageCss1Str="";
  100. $subPageCss1Str.="共".$this->nums."条记录,";
  101. $subPageCss1Str.="每页显示".$this->each_disNums."条,";
  102. $subPageCss1Str.="当前第".$this->current_page."/".$this->pageNums."页 ";
  103. if($this->current_page > 1){
  104. $firstPageUrl=$this->subPage_link."1";
  105. $prewPageUrl=$this->subPage_link.($this->current_page-1);
  106. $subPageCss1Str.="[<a href='$firstPageUrl'>首页</a>] ";
  107. $subPageCss1Str.="[<a href='$prewPageUrl'>上一页</a>] ";
  108. }else {
  109. $subPageCss1Str.="[首页] ";
  110. $subPageCss1Str.="[上一页] ";
  111. }
  112. if($this->current_page < $this->pageNums){
  113. $lastPageUrl=$this->subPage_link.$this->pageNums;
  114. $nextPageUrl=$this->subPage_link.($this->current_page+1);
  115. $subPageCss1Str.=" [<a href='$nextPageUrl'>下一页</a>] ";
  116. $subPageCss1Str.="[<a href='$lastPageUrl'>尾页</a>] ";
  117. }else {
  118. $subPageCss1Str.="[下一页] ";
  119. $subPageCss1Str.="[尾页] ";
  120. }
  121. echo $subPageCss1Str;
  122. }
  123. /*
  124. 构造经典模式的分页
  125. 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
  126. *//* 产品页用*/
  127. function subPageCss2(){
  128. $subPageCss2Str="";
  129. //jason edit
  130. //$subPageCss2Str.="<li class="text">当前第".$this->current_page."/".$this->pageNums."页 </li>";
  131. if($this->current_page > 1){
  132. $firstPageUrl=$this->subPage_link."1";
  133. $prewPageUrl=$this->subPage_link.($this->current_page-1);
  134. //$subPageCss2Str.="<li class="pre"><a href='$firstPageUrl'>首 页</a></li>";
  135. $subPageCss2Str.="<li class="pre"><a href='$prewPageUrl'>上一页</a></li>";
  136. }else {
  137. //$subPageCss2Str.="<li class="prea">首 页</li> ";
  138. $subPageCss2Str.="<li class="prea">上一页</li> ";
  139. }
  140. $a=$this->construct_num_Page();
  141. for($i=0;$i<count($a);$i++){
  142. $s = $a[$i];
  143. if($s == $this->current_page ){
  144. $subPageCss2Str.="<li class="num">".$s."</li>";
  145. }else{
  146. $url=$this->subPage_link.$s;
  147. //echo $url;exit;
  148. $subPageCss2Str.="<li class="num2"><a href='$url'>".$s."</a></li>";
  149. //echo $subPageCss2Str;
  150. }
  151. }
  152. //exit;
  153. if($this->current_page < $this->pageNums){
  154. $lastPageUrl=$this->subPage_link.$this->pageNums;
  155. $nextPageUrl=$this->subPage_link.($this->current_page+1);
  156. $subPageCss2Str.=" <li class="next"><a href='$nextPageUrl'>下一页</a></li> ";//开源代码phpfensi.com
  157. //$subPageCss2Str.="<li class="next"><a href='$lastPageUrl'>尾 页</a></li> ";
  158. }else {
  159. $subPageCss2Str.="<li class="nexta">下一页</li>";
  160. //$subPageCss2Str.="<li class="nexta">尾 页</li> ";
  161. }
  162. echo $subPageCss2Str;
  163. }
  164. }