当前位置:Gxlcms > PHP教程 > 分页类——尾部页码导航

分页类——尾部页码导航

时间:2021-07-01 10:21:17 帮助过:18人阅读

分页类——尾部页码导航
  1. /**
  2. * 生成Comment的尾部分页导航
  3. * @author 李俊
  4. *
  5. */
  6. class cmtTail{
  7. private $currentPage;
  8. private $totalPage;
  9. /**
  10. * 生成页码导航--总控函数
  11. * @param string $currentPage 当前页码
  12. * @param string $totalPage 总页数
  13. * @throws Exception 页码小于1将会抛出异常
  14. * @return string
  15. */
  16. function __do($currentPage, $totalPage) {
  17. $this->currentPage=$currentPage;
  18. $this->totalPage=$totalPage;
  19. if($this->totalPage<=10){//总页数小于等于10页
  20. if($this->currentPage==1){//当前页是第一页
  21. $str='上一页'.$this->currentTag();
  22. for ($i = 2; $i <= $this->totalPage; $i++) {
  23. $str=$str.$this->commonTag($i);
  24. }
  25. $str=$str.$this->next();
  26. }elseif ($this->currentPage==$this->totalPage){//已跳至最后一页
  27. $str=$this->up();
  28. for ($i = 1; $i <= $this->totalPage-1; $i++) {
  29. $str=$str.$this->commonTag($i);
  30. }
  31. $str=$str.$this->currentTag();
  32. $str=$str.$this->next();
  33. }else{
  34. $str=$this->up();
  35. for($i=1; $i<$this->currentPage; $i++){
  36. $str=$str.$this->commonTag($i);
  37. }
  38. $str=$str.$this->currentTag();//生成当前页标签
  39. for ($i = $this->currentPage+1; $i <= $this->totalPage; $i++) {
  40. $str=$str.$this->commonTag($i);
  41. }
  42. $str=$str.$this->next();
  43. }
  44. }elseif ($this->totalPage>10){//页码大于10
  45. if($this->currentPage==1){//8+2
  46. $str='上一页'.$this->currentTag();
  47. for ($i = 2; $i <= 8; $i++) {
  48. $str=$str.$this->commonTag($i);
  49. }
  50. $str=$str.'...';//添加省略号
  51. $str=$str.$this->commonTag($this->totalPage-1);
  52. $str=$str.$this->commonTag($this->totalPage);
  53. }elseif($this->currentPage==$this->totalPage) {//当前为最后一页
  54. $str=$this->up();
  55. $str=$str.$this->commonTag(1);
  56. $str=$str.'...';//添加省略号
  57. for ($i = $this->totalPage-6; $i < $this->totalPage; $i++) {
  58. $str=$str.$this->commonTag($i);
  59. }
  60. $str=$str.$this->currentTag();
  61. $str=$str.$this->next();
  62. }else {
  63. if ($this->currentPage<6) {
  64. $str=$this->up();
  65. for ($i = 1; $i < $this->currentPage; $i++) {
  66. $str=$str.$this->commonTag($i);
  67. }
  68. $str=$str.$this->currentTag();
  69. for ($i = $this->currentPage+1; $i <= 7; $i++) {
  70. $str=$str.$this->commonTag($i);
  71. }
  72. $str=$str.'...';//添加省略号
  73. $str=$str.$this->commonTag($this->totalPage);
  74. $str=$str.$this->next();
  75. }else {
  76. if ($this->currentPage>=$this->totalPage-4) {
  77. $str=$this->up();
  78. $str=$str.$this->commonTag(1);
  79. $str=$str.'...';//添加省略号
  80. for ($i = $this->totalPage-6; $i < $this->currentPage; $i++) {
  81. $str=$str.$this->commonTag($i);
  82. }
  83. $str=$str.$this->currentTag();
  84. for ($i = $this->currentPage+1; $i <= $this->totalPage; $i++) {
  85. $str=$str.$this->commonTag($i);
  86. }
  87. $str=$str.$this->next();
  88. }elseif ($this->currentPage<$this->totalPage-4){//1+5+1
  89. $str=$this->up();
  90. $str=$str.$this->commonTag(1);
  91. $str=$str.'...';//添加省略号
  92. $str=$str.$this->commonTag($this->currentPage-2);
  93. $str=$str.$this->commonTag($this->currentPage-1);
  94. $str=$str.$this->currentTag();
  95. $str=$str.$this->commonTag($this->currentPage+1);
  96. $str=$str.$this->commonTag($this->currentPage+2);
  97. $str=$str.'...';//添加省略号
  98. $str=$str.$this->commonTag($this->totalPage);
  99. $str=$str.$this->next();
  100. }
  101. };
  102. }
  103. }elseif ($this->totalPage<=0){
  104. throw new Exception("页面不可能小于1");
  105. }
  106. return $str;
  107. }
  108. /**
  109. * 一般标签
  110. * @param int $param 页码
  111. * @return string
  112. */
  113. function commonTag($param) {
  114. return ''.$param.'';
  115. }
  116. /**
  117. * 生成当前页标签
  118. * @param int $param 页码
  119. * @return string
  120. */
  121. function currentTag() {
  122. return ''.$this->currentPage.'';
  123. }
  124. /**
  125. * 生成下一页标签
  126. * @param int $param 下一页页码
  127. * @return string
  128. */
  129. function next() {
  130. if ($this->currentPage==$this->totalPage) {
  131. return '下一页';
  132. }
  133. return 'currentPage+1).'">下一页';
  134. }
  135. /**
  136. * 生成上一页标签
  137. * @param int $param 上一页页码
  138. * @return string
  139. */
  140. function up() {
  141. if ($this->currentPage==1){
  142. return '上一页';
  143. }else{
  144. return 'currentPage-1).'">上一页';
  145. }
  146. }
  147. /**
  148. * 实例化cmtTail,
  149. * 功能:生成Comment的尾部分页导航
  150. * @param string $currentPage 当前页码
  151. * @param string $totalPage 总页数
  152. * @return string 返回html标签字符串
  153. */
  154. static function GO($currentPage, $totalPage) {
  155. $p=new cmtTail();
  156. return $p->__do($currentPage, $totalPage);
  157. }
  158. }

人气教程排行