Page.class.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace KIF\Page;
  3. /**
  4. * 分页类
  5. * 需设定以下样式:
  6. * page-prev:上一页
  7. * page-next:下一页
  8. * page-more:更多,即...
  9. * page-current:当前页
  10. * @author gaoxiaogang@gmail.com
  11. */
  12. use KIF\Verify;
  13. use Exception;
  14. class Page {
  15. /**
  16. *
  17. * 总页数
  18. * @var int
  19. */
  20. protected $pages;
  21. /**
  22. *
  23. * 当前页
  24. * @var int
  25. */
  26. protected $page;
  27. protected $first_num = 2;
  28. protected $end_num = 2;
  29. protected $nearby_num = 2;
  30. /**
  31. *
  32. * url模版
  33. * 必须用 {page} 代表页码占位符。比如 http://misc.kimiss.com/comments/19933/{page}
  34. * @var string
  35. */
  36. protected $url_tpl;
  37. /**
  38. *
  39. * Enter description here ...
  40. * @param int $nums 总结果数量
  41. * @param string $url_tpl url模版,必须用 {page} 代表页码占位符。比如 http://misc.kimiss.com/comments/19933/{page}
  42. * @param int $page 当前页码
  43. * @param int $size 每页显示多少个
  44. * @return Page
  45. */
  46. public function __construct($nums, $url_tpl, $cur_page = 1, $size = 20) {
  47. if (!Verify::unsignedInt($cur_page)) {
  48. throw new Exception("invalid cur_page param: {$cur_page}");
  49. }
  50. $this->pages = ceil($nums/$size);
  51. if ($cur_page > $this->pages) {
  52. $cur_page = $this->pages;
  53. }
  54. $this->page = $cur_page;
  55. $this->url_tpl = $url_tpl;
  56. }
  57. /**
  58. *
  59. * 获取指定页面的url
  60. * @param int $page
  61. * @return string
  62. */
  63. private function pageUrl($page) {
  64. $url = str_replace('{page}', $page, $this->url_tpl);
  65. $url = str_replace('%7Bpage%7D', $page, $url);// 支持url被转义后的
  66. return $url;
  67. }
  68. public function html() {
  69. $html = '';
  70. # 显示上一页
  71. if ($this->page > 1) {
  72. $prev_page = $this->page - 1;
  73. $html .= <<<PAGE
  74. <a class="page-prev" data-page="{$prev_page}" href="{$this->pageUrl($prev_page)}">
  75. 上一页
  76. <i></i>
  77. </a>
  78. PAGE;
  79. }
  80. # 显示第1、2页
  81. for ($i=1; $i<=$this->first_num; $i++) {
  82. if ($i >= $this->page-$this->nearby_num) {
  83. continue;
  84. }
  85. $html .= <<<PAGE
  86. <a data-page="{$i}" href="{$this->pageUrl($i)}">
  87. {$i}
  88. </a>
  89. PAGE;
  90. }
  91. if ($this->page > ($this->first_num+$this->nearby_num+1)) {
  92. $html .= '<span class="page-more">...</span>';
  93. }
  94. for ($i=$this->page-$this->nearby_num; $i <= $this->page+$this->nearby_num; $i++ ) {
  95. if ($i < 1) {
  96. continue;
  97. }
  98. if ($i > $this->pages) {
  99. continue;
  100. }
  101. if ($i == $this->page) {
  102. $html .= "<span class=\"page-current\">{$i}</span>";
  103. } else {
  104. $html .= <<<PAGE
  105. <a data-page="{$i}" href="{$this->pageUrl($i)}">
  106. {$i}
  107. </a>
  108. PAGE;
  109. }
  110. }
  111. if ($this->page < $this->pages-$this->end_num-$this->nearby_num) {
  112. $html .= '<span class="page-more">...</span>';
  113. }
  114. # 显示倒数第1、2页
  115. for ($i=$this->pages-$this->end_num+1; $i<=$this->pages; $i++) {
  116. if ($i <= $this->page+$this->nearby_num) {
  117. continue;
  118. }
  119. $html .= <<<PAGE
  120. <a data-page="{$i}" href="{$this->pageUrl($i)}">
  121. {$i}
  122. </a>
  123. PAGE;
  124. }
  125. # 显示下一页
  126. if ($this->page < $this->pages) {
  127. $next_page = $this->page + 1;
  128. $html .= <<<PAGE
  129. <a class="page-next" data-page="{$next_page}" href="{$this->pageUrl($next_page)}">
  130. 下一页
  131. <i></i>
  132. </a>
  133. PAGE;
  134. }
  135. return $html;
  136. }
  137. }