Heim  >  Artikel  >  Backend-Entwicklung  >  php分页类代码,支持伪静态的php分页类

php分页类代码,支持伪静态的php分页类

WBOY
WBOYOriginal
2016-07-25 08:52:351221Durchsuche
  1. class Page{
  2. protected $each_disNums;//每页显示的条目数
  3. protected $nums;//总条目数
  4. protected $current_page;//当前被选中的页
  5. protected $sub_pages;//每次显示的页数
  6. protected $pageNums;//总页数
  7. protected $page_array = array();//用来构造分页的数组
  8. protected $subPage_link;//每个分页的链接
  9. protected $subPage_type;//显示分页的类型
  10. protected $houz;//后缀
  11. /*
  12. __construct是SubPages的构造函数,用来在创建类的时候自动运行.
  13. @$each_disNums每页显示的条目数
  14. @nums 总条目数
  15. @current_num 当前被选中的页
  16. @sub_pages每次显示的页数
  17. @subPage_link每个分页的链接
  18. @subPage_type显示分页的类型
  19. 当@subPage_type=1的时候为普通分页模式
  20. example:共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页]
  21. 当@subPage_type=2的时候为经典分页样式
  22. example:当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
  23. */
  24. function __construct($each_disNums,$nums,$current_page,$sub_pages,$subPage_link,$subPage_type,$houz=''){
  25. $this->each_disNums=intval($each_disNums);
  26. $this->nums=intval($nums);
  27. if(!$current_page){
  28. $this->current_page=1;
  29. }else{
  30. $this->current_page=intval($current_page);
  31. }
  32. $this->sub_pages=intval($sub_pages);
  33. $this->pageNums=ceil($nums/$each_disNums);
  34. $this->subPage_link=$subPage_link;
  35. $this->show_SubPages($subPage_type);
  36. $this->houz=$houz;
  37. //echo $this->pageNums."--".$this->sub_pages;
  38. }
  39. /*
  40. __destruct析构函数,当类不在使用的时候调用,该函数用来释放资源。
  41. */
  42. function __destruct(){
  43. unset($each_disNums);
  44. unset($nums);
  45. unset($current_page);
  46. unset($sub_pages);
  47. unset($pageNums);
  48. unset($page_array);
  49. unset($subPage_link);
  50. unset($subPage_type);
  51. }
  52. /*
  53. show_SubPages函数用在构造函数里面。而且用来判断显示什么样子的分页
  54. */
  55. function show_SubPages($subPage_type){
  56. if($subPage_type == 1){
  57. $this->subPageCss1();
  58. }elseif ($subPage_type == 2){
  59. $this->subPageCss2();
  60. }
  61. }
  62. /*
  63. 用来给建立分页的数组初始化的函数。
  64. */
  65. function initArray(){
  66. for($i=0;$isub_pages;$i++){
  67. $this->page_array[$i]=$i;
  68. }
  69. return $this->page_array;
  70. }
  71. /*
  72. construct_num_Page该函数使用来构造显示的条目
  73. 即使:[1][2][3][4][5][6][7][8][9][10]
  74. */
  75. function construct_num_Page(){
  76. if($this->pageNums sub_pages){
  77. $current_array=array();
  78. for($i=0;$ipageNums;$i++){
  79. $current_array[$i]=$i+1;
  80. }
  81. }else{
  82. $current_array=$this->initArray();
  83. if($this->current_page for($i=0;$i $current_array[$i]=$i+1;
  84. }
  85. }elseif ($this->current_page pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1 ){
  86. for($i=0;$i $current_array[$i]=($this->pageNums)-($this->sub_pages)+1+$i;
  87. }
  88. }else{
  89. for($i=0;$i $current_array[$i]=$this->current_page-2+$i;
  90. }
  91. }
  92. }
  93. return $current_array;
  94. }
  95. /*
  96. 构造普通模式的分页
  97. 共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页]
  98. */ bbs.it-home.org
  99. function subPageCss1(){
  100. $subPageCss1Str="";
  101. $subPageCss1Str.="共".$this->nums."条记录,";
  102. $subPageCss1Str.="每页显示".$this->each_disNums."条,";
  103. $subPageCss1Str.="当前第".$this->current_page."/".$this->pageNums."页 ";
  104. if($this->current_page > 1){
  105. $firstPageUrl=$this->subPage_link."1".$this->houz;
  106. $prewPageUrl=$this->subPage_link.($this->current_page-1).$this->houz;
  107. $subPageCss1Str.="[首页] ";
  108. $subPageCss1Str.="[上一页] ";
  109. }else {
  110. $subPageCss1Str.="[首页] ";
  111. $subPageCss1Str.="[上一页] ";
  112. }
  113. if($this->current_page pageNums){
  114. $lastPageUrl=$this->subPage_link.$this->pageNums.$this->houz;
  115. $nextPageUrl=$this->subPage_link.($this->current_page+1).$this->houz;
  116. $subPageCss1Str.=" [下一页] ";
  117. $subPageCss1Str.="[尾页] ";
  118. }else {
  119. $subPageCss1Str.="[下一页] ";
  120. $subPageCss1Str.="[尾页] ";
  121. }
  122. return $subPageCss1Str;
  123. }
  124. /*
  125. 构造经典模式的分页
  126. 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
  127. */
  128. function subPageCss2(){
  129. $subPageCss2Str="";
  130. $subPageCss2Str.="共[".$this->nums."]条 当前第".$this->current_page."/".$this->pageNums."页";
  131. if($this->current_page > 1){
  132. $firstPageUrl=$this->subPage_link."1".$this->houz;
  133. $prewPageUrl=$this->subPage_link.($this->current_page-1).$this->houz;
  134. $subPageCss2Str.="[首页] ";
  135. $subPageCss2Str.="[上一页] ";
  136. }else {
  137. $subPageCss2Str.="[首页] ";
  138. $subPageCss2Str.="[上一页] ";
  139. }
  140. $a=$this->construct_num_Page();
  141. for($i=0;$i$s=$a[$i];
  142. if($s == $this->current_page ){
  143. $subPageCss2Str.="[".$s."]";
  144. }else{
  145. $url=$this->subPage_link.$s.$this->houz;
  146. $subPageCss2Str.="[".$s."]";
  147. }
  148. }
  149. if($this->current_page pageNums){
  150. $lastPageUrl=$this->subPage_link.$this->pageNums.$this->houz;
  151. $nextPageUrl=$this->subPage_link.($this->current_page+1).$this->houz;
  152. $subPageCss2Str.=" [下一页] ";
  153. $subPageCss2Str.="[尾页] ";
  154. }else {
  155. $subPageCss2Str.="[下一页] ";
  156. $subPageCss2Str.="[尾页] ";
  157. }
  158. return $subPageCss2Str;
  159. }
  160. /*
  161. 构造经典模式ajax的分页
  162. 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
  163. */
  164. function subPageCss3($fun='',$v='n'){
  165. $subPageCss2Str="";
  166. $subPageCss2Str.="共[".$this->nums."]条 当前第".$this->current_page."/".$this->pageNums."页";
  167. if($this->current_page > 1){
  168. //$firstPageUrl=$this->subPage_link."1";
  169. $prewPageUrl=$this->current_page-1;
  170. $subPageCss2Str.="[首页] ";
  171. $subPageCss2Str.="[上一页] ";
  172. }else {
  173. $subPageCss2Str.="[首页] ";
  174. $subPageCss2Str.="[上一页] ";
  175. }
  176. $a=$this->construct_num_Page();
  177. for($i=0;$i$s=$a[$i];
  178. if($s == $this->current_page ){
  179. $subPageCss2Str.="[$s]";
  180. }else{
  181. $subPageCss2Str.="[$s]";
  182. }
  183. }
  184. if($this->current_page pageNums){
  185. $lastPageUrl=$this->pageNums;
  186. $nextPageUrl=$this->current_page+1;
  187. $subPageCss2Str.=" [下一页] ";
  188. $subPageCss2Str.="[尾页] ";
  189. }else {
  190. $subPageCss2Str.="[下一页] ";
  191. $subPageCss2Str.="[尾页] ";
  192. }
  193. return $subPageCss2Str;
  194. }
  195. }
  196. ?>
复制代码

使用方法: 实例化一下这个page类, $list = new Page(每页多少条数据,数据总量,当前页,用来构造分页的数组,每个分页的链接 ,显示分页的类型,后缀 ); 比如 $list = new Page(10,1000,1,10,’/zjjz/’,2,’.html’); echo $page = $list->subPageCss2(); 就会显示: 当前第1/100页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页] 每个连接就是 /zjjz/1.html,/zjjz/2.html……. 可以根据直接需要,来组合这个每个分页的链接,实现伪静态。

而ajax分页,就是

$list = new Page($pagesize,$areaAllNumber,$current_page,10,”,3); $url = $val1.’,’.$val2;//这个就是你要触发的js函数中要传递的值, $page = $list->subPageCss3(‘checkProducts’,$url);//第一个参数就是要触发的js函数。从而达到ajax分页的效果。

还有一个subPageCss1是 共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页] 此种简单样式,也支持伪静态。



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn