Pagination.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. class LtPagination
  3. {
  4. public $configHandle;
  5. public $conf;
  6. public function __construct()
  7. {
  8. if (! $this->configHandle instanceof LtConfig)
  9. {
  10. if (class_exists("LtObjectUtil", false))
  11. {
  12. $this->configHandle = LtObjectUtil::singleton("LtConfig");
  13. }
  14. else
  15. {
  16. $this->configHandle = new LtConfig;
  17. }
  18. }
  19. }
  20. public function init()
  21. {
  22. $this->conf = $this->configHandle->get("pagination.pager");
  23. if (empty($this->conf))
  24. {
  25. $this->conf['per_page'] = 25; //每个页面中希望展示的项目数量
  26. $this->conf['num_links_show'] = 9; //数字链接显示数量
  27. $this->conf['num_point_start_end'] = 2; //“点”前边和后边的链接数量
  28. $this->conf['show_first'] = true;
  29. $this->conf['show_prev'] = true;
  30. $this->conf['show_next'] = true;
  31. $this->conf['show_last'] = true;
  32. $this->conf['show_goto'] = false;
  33. $this->conf['show_info'] = false;
  34. $this->conf['show_point'] = true;
  35. $this->conf['show_empty_button'] = false;
  36. $this->conf['first_text'] = 'First';
  37. $this->conf['prev_text'] = 'Prev';
  38. $this->conf['next_text'] = 'Next';
  39. $this->conf['last_text'] = 'Last';
  40. $this->conf['point_text'] = '...';
  41. $this->conf['full_tag_open'] = '<div class="pages">';
  42. $this->conf['full_tag_close'] = '</div>';
  43. $this->conf['num_tag_open'] = '';
  44. $this->conf['num_tag_close'] = '';
  45. $this->conf['link_tag_open'] = '<a href=":url">';
  46. $this->conf['link_tag_close'] = '</a>';
  47. $this->conf['link_tag_cur_open'] = '<strong>';
  48. $this->conf['link_tag_cur_close'] = '</strong>';
  49. $this->conf['button_tag_open'] = '<a href=":url" style="font-weight:bold">';
  50. $this->conf['button_tag_close'] = '</a>';
  51. $this->conf['button_tag_empty_open'] = '<span>';
  52. $this->conf['button_tag_empty_close'] = '</span>';
  53. $this->conf['point_tag_open'] = '<span>';
  54. $this->conf['point_tag_close'] = '</span>';
  55. }
  56. }
  57. /**
  58. *
  59. * @param $page int 当前页
  60. * @param $count int 这个数值是你查询数据库得到的数据总量
  61. * @param $url string 字串中使用 :page 表示页参数 不在意位置 如/a/:page/c/e
  62. */
  63. public function pager($page, $count, $url)
  64. {
  65. $per_page = empty($this->conf['per_page']) ? 25 : $this->conf['per_page'];
  66. $pagecount = ceil($count / $per_page);
  67. $pager = $this->renderPager($page, $pagecount, $url);
  68. if ($this->conf['show_goto'])
  69. {
  70. $pager .= $this->renderButton('goto', $page, $pagecount, $url);
  71. }
  72. if ($this->conf['show_info'])
  73. {
  74. $pager .= $this->renderButton('info', $page, $pagecount, $url);
  75. }
  76. return $this->conf['full_tag_open'] . $pager . $this->conf['full_tag_close'];
  77. }
  78. /**
  79. *
  80. * @param $pagenumber int 当前页
  81. * @param $pagecount int 总页数
  82. * @return string
  83. */
  84. public function renderPager($pagenumber, $pagecount, $baseurl = '?page=:page')
  85. {
  86. $baseurl = urldecode($baseurl);
  87. $pager = $this->conf['num_tag_open'];
  88. $pager .= $this->renderButton('first', $pagenumber, $pagecount, $baseurl);
  89. $pager .= $this->renderButton('prev', $pagenumber, $pagecount, $baseurl);
  90. $startPoint = 1;
  91. $endPoint = $this->conf['num_links_show'];
  92. $num_links = ceil($this->conf['num_links_show'] / 2) - 1;
  93. if ($pagenumber > $num_links)
  94. {
  95. $startPoint = $pagenumber - $num_links;
  96. $endPoint = $pagenumber + $num_links;
  97. }
  98. if ($endPoint > $pagecount)
  99. {
  100. $startPoint = $pagecount + 1 - $this->conf['num_links_show'];
  101. $endPoint = $pagecount;
  102. }
  103. if ($startPoint < 1)
  104. {
  105. $startPoint = 1;
  106. }
  107. $currentButton = '';
  108. if ($this->conf['show_point'])
  109. {
  110. for($page = 1; $page < $startPoint; $page++)
  111. {
  112. $url = str_replace(':page', $page, $baseurl);
  113. if ($page > $this->conf['num_point_start_end'])
  114. {
  115. $currentButton .= $this->conf['point_tag_open'] . $this->conf['point_text'] . $this->conf['point_tag_close'];
  116. break;
  117. }
  118. $currentButton .= str_replace(':url', $url, $this->conf['link_tag_open']) . $page . $this->conf['link_tag_close'];
  119. }
  120. }
  121. for ($page = $startPoint; $page <= $endPoint; $page++)
  122. {
  123. $url = str_replace(':page', $page, $baseurl);
  124. if ($page == $pagenumber)
  125. {
  126. $currentButton .= $this->conf['link_tag_cur_open'] . $page . $this->conf['link_tag_cur_close'];
  127. }
  128. else
  129. {
  130. $currentButton .= str_replace(':url', $url, $this->conf['link_tag_open']) . $page . $this->conf['link_tag_close'];
  131. }
  132. }
  133. if ($this->conf['show_point'])
  134. {
  135. $page = $pagecount - $this->conf['num_point_start_end'];
  136. if ($page > $endPoint)
  137. {
  138. $currentButton .= $this->conf['point_tag_open'] . $this->conf['point_text'] . $this->conf['point_tag_close'];
  139. }
  140. for($page += 1; $page >= $endPoint && $page <= $pagecount; $page++)
  141. {
  142. if ($page == $endPoint) continue;
  143. $url = str_replace(':page', $page, $baseurl);
  144. $currentButton .= str_replace(':url', $url, $this->conf['link_tag_open']) . $page . $this->conf['link_tag_close'];
  145. }
  146. }
  147. $pager .= $currentButton;
  148. $pager .= $this->renderButton('next', $pagenumber, $pagecount, $baseurl);
  149. $pager .= $this->renderButton('last', $pagenumber, $pagecount, $baseurl);
  150. $pager .= $this->conf['num_tag_close'];
  151. return $pager;
  152. }
  153. /**
  154. *
  155. * @param $buttonLabel string 显示文字
  156. * @param $pagenumber int 当前页
  157. * @param $pagecount int 总页数
  158. * @return string
  159. */
  160. public function renderButton($buttonLabel, $pagenumber, $pagecount, $baseurl = '?page=:page')
  161. {
  162. $baseurl = urldecode($baseurl);
  163. $destPage = 1;
  164. if ('goto' == $buttonLabel)
  165. {
  166. $button = "goto <input type=\"text\" size=\"3\" onkeydown=\"javascript: if(event.keyCode==13){ location='{$baseurl}'.replace(':page',this.value);return false;}\" />";
  167. return $button;
  168. }
  169. if ('info' == $buttonLabel)
  170. {
  171. $button = " $pagenumber/$pagecount ";
  172. return $button;
  173. }
  174. switch ($buttonLabel)
  175. {
  176. case "first":
  177. $destPage = 1;
  178. $bottenText = $this->conf['first_text'];
  179. break;
  180. case "prev":
  181. $destPage = $pagenumber - 1;
  182. $bottenText = $this->conf['prev_text'];
  183. break;
  184. case "next":
  185. $destPage = $pagenumber + 1;
  186. $bottenText = $this->conf['next_text'];
  187. break;
  188. case "last":
  189. $destPage = $pagecount;
  190. $bottenText = $this->conf['last_text'];
  191. break;
  192. }
  193. $url = str_replace(':page', $destPage, $baseurl);
  194. $button = str_replace(':url', $url, $this->conf['button_tag_open']) . $bottenText . $this->conf['button_tag_close'];
  195. if ($buttonLabel == "first" || $buttonLabel == "prev")
  196. {
  197. if ($pagenumber <= 1)
  198. {
  199. $button = $this->conf['show_empty_button'] ? $this->conf['button_tag_empty_open'] . $bottenText . $this->conf['button_tag_empty_close'] : '';
  200. }
  201. }
  202. else
  203. {
  204. if ($pagenumber >= $pagecount)
  205. {
  206. $button = $this->conf['show_empty_button'] ? $this->conf['button_tag_empty_open'] . $bottenText . $this->conf['button_tag_empty_close'] : '';
  207. }
  208. }
  209. return $button;
  210. }
  211. }