Pagination.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. /**
  3. * 第三方分页类.
  4. * User: yqj
  5. * Date: 2017/5/3
  6. * Time: 17:00
  7. */
  8. /**
  9. * Pagination
  10. *
  11. * Supplies an API for setting pagination details, and renders the resulting
  12. * pagination markup (html) through the included render.inc.php file.
  13. *
  14. * @note The SEO methods (canonical/rel) were written following Google's
  15. * suggested patterns. Namely, the canoical url excludes any
  16. * peripheral parameters that don't relate to the pagination
  17. * series. Whereas the prev/next rel link tags include any params
  18. * found in the request.
  19. * @author Oliver Nassar <onassar@gmail.com>
  20. * @todo add setter parameter type and range checks w/ exceptions
  21. * @example
  22. * <code>
  23. * // source inclusion
  24. * require_once APP . '/vendors/PHP-Pagination/Pagination.class.php';
  25. *
  26. * // determine page (based on <_GET>)
  27. * $page = isset($_GET['page']) ? ((int) $_GET['page']) : 1;
  28. *
  29. * // instantiate with page and records as constructor parameters
  30. * $pagination = (new Pagination($page, 200));
  31. * $markup = $pagination->parse();
  32. * </code>
  33. * @example
  34. * <code>
  35. * // source inclusion
  36. * require_once APP . '/vendors/PHP-Pagination/Pagination.class.php';
  37. *
  38. * // determine page (based on <_GET>)
  39. * $page = isset($_GET['page']) ? ((int) $_GET['page']) : 1;
  40. *
  41. * // instantiate; set current page; set number of records
  42. * $pagination = (new Pagination());
  43. * $pagination->setCurrent($page);
  44. * $pagination->setTotal(200);
  45. *
  46. * // grab rendered/parsed pagination markup
  47. * $markup = $pagination->parse();
  48. * </code>
  49. */
  50. namespace app\Lib;
  51. class Pagination
  52. {
  53. public $first_row; //起始行数
  54. public $list_rows; //列表每页显示行数
  55. protected $total_pages; //总页数
  56. protected $total_rows; //总行数
  57. protected $now_page; //当前页数
  58. protected $method = 'defalut'; //处理情况 Ajax分页 Html分页(静态化时) 普通get方式
  59. protected $parameter = '';
  60. protected $page_name; //分页参数的名称
  61. protected $ajax_func_name;
  62. public $plus = 3; //分页偏移量
  63. protected $url;
  64. /**
  65. * 构造函数
  66. * @param unknown_type $data
  67. */
  68. public function __construct($data = array())
  69. {
  70. $this->total_rows = $data['total_rows'];
  71. $this->parameter = !empty($data['parameter']) ? $data['parameter'] : '';
  72. $this->list_rows = !empty($data['list_rows']) && $data['list_rows'] <= 100 ? $data['list_rows'] : 15;
  73. $this->total_pages = ceil($this->total_rows / $this->list_rows);
  74. $this->page_name = !empty($data['page_name']) ? $data['page_name'] : 'p';
  75. $this->ajax_func_name = !empty($data['ajax_func_name']) ? $data['ajax_func_name'] : '';
  76. $this->method = !empty($data['method']) ? $data['method'] : '';
  77. /* 当前页面 */
  78. if(!empty($data['now_page']))
  79. {
  80. $this->now_page = intval($data['now_page']);
  81. }else{
  82. $this->now_page = !empty($_GET[$this->page_name]) ? intval($_GET[$this->page_name]):1;
  83. }
  84. $this->now_page = $this->now_page <= 0 ? 1 : $this->now_page;
  85. if(!empty($this->total_pages) && $this->now_page > $this->total_pages)
  86. {
  87. $this->now_page = $this->total_pages;
  88. }
  89. $this->first_row = $this->list_rows * ($this->now_page - 1);
  90. }
  91. /**
  92. * 得到当前连接
  93. * @param $page
  94. * @param $text
  95. * @return string
  96. */
  97. protected function _get_link($page,$text)
  98. {
  99. switch ($this->method) {
  100. case 'ajax':
  101. $parameter = '';
  102. if($this->parameter)
  103. {
  104. $parameter = ','.$this->parameter;
  105. }
  106. return '<a onclick="' . $this->ajax_func_name . '(\'' . $page . '\''.$parameter.')" href="javascript:void(0)">' . $text . '</a>' . "\n";
  107. break;
  108. case 'html':
  109. $url = str_replace('@', $page,$this->parameter);
  110. return '<li class="page-item"><a href="' .$url . '">' . $text . '</a>' . "</li>";
  111. break;
  112. default:
  113. return '<a href="' . $this->_get_url($page) . '">' . $text . '</a>' . "\n";
  114. break;
  115. }
  116. }
  117. /**
  118. * 设置当前页面链接
  119. */
  120. protected function _set_url()
  121. {
  122. $url = $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;
  123. $parse = parse_url($url);
  124. if(isset($parse['query'])) {
  125. parse_str($parse['query'],$params);
  126. unset($params[$this->page_name]);
  127. $url = $parse['path'].'?'.http_build_query($params);
  128. }
  129. if(!empty($params))
  130. {
  131. $url .= '&';
  132. }
  133. $this->url = $url;
  134. }
  135. /**
  136. * 得到$page的url
  137. * @param $page 页面
  138. * @return string
  139. */
  140. protected function _get_url($page)
  141. {
  142. if($this->url === NULL)
  143. {
  144. $this->_set_url();
  145. }
  146. // $lable = strpos('&', $this->url) === FALSE ? '' : '&';
  147. return $this->url . $this->page_name . '=' . $page;
  148. }
  149. /**
  150. * 得到第一页
  151. * @return string
  152. */
  153. public function first_page($name = '第一页')
  154. {
  155. if($this->now_page > 5)
  156. {
  157. return $this->_get_link('1', $name);
  158. }
  159. return '';
  160. }
  161. /**
  162. * 最后一页
  163. * @param $name
  164. * @return string
  165. */
  166. public function last_page($name = '最后一页')
  167. {
  168. if($this->now_page < $this->total_pages - 5)
  169. {
  170. return $this->_get_link($this->total_pages, $name);
  171. }
  172. return '';
  173. }
  174. /**
  175. * 上一页
  176. * @return string
  177. */
  178. public function up_page($name = '上一页')
  179. {
  180. if($this->now_page != 1)
  181. {
  182. return $this->_get_link($this->now_page - 1, $name);
  183. }
  184. return '';
  185. }
  186. /**
  187. * 下一页
  188. * @return string
  189. */
  190. public function down_page($name = '下一页')
  191. {
  192. if($this->now_page < $this->total_pages)
  193. {
  194. return $this->_get_link($this->now_page + 1, $name);
  195. }
  196. return '';
  197. }
  198. /**
  199. * 分页样式输出
  200. * @param $param
  201. * @return string
  202. */
  203. public function show($param = 1)
  204. {
  205. if($this->total_rows < 1)
  206. {
  207. return '';
  208. }
  209. $className = 'show_' . $param;
  210. $classNames = get_class_methods($this);
  211. if(in_array($className, $classNames))
  212. {
  213. return $this->$className();
  214. }
  215. return '';
  216. }
  217. protected function show_2()
  218. {
  219. if($this->total_pages != 1)
  220. {
  221. $return = '';
  222. $return .= $this->up_page('<');
  223. for($i = 1;$i<=$this->total_pages;$i++)
  224. {
  225. if($i == $this->now_page)
  226. {
  227. $return .= "<li class='page-item active'><a class='page-link'>$i</a></li>";
  228. }
  229. else
  230. {
  231. if($this->now_page-$i>=4 && $i != 1)
  232. {
  233. $return .="<span class='pageMore'>...</span>\n";
  234. $i = $this->now_page-3;
  235. }
  236. else
  237. {
  238. if($i >= $this->now_page+5 && $i != $this->total_pages)
  239. {
  240. $return .="<span>...</span>\n";
  241. $i = $this->total_pages;
  242. }
  243. $return .= $this->_get_link($i, $i) . "\n";
  244. }
  245. }
  246. }
  247. $return .= $this->down_page('>');
  248. return $return;
  249. }
  250. }
  251. protected function show_1()
  252. {
  253. $plus = $this->plus;
  254. if( $plus + $this->now_page > $this->total_pages)
  255. {
  256. $begin = $this->total_pages - $plus * 2;
  257. }else{
  258. $begin = $this->now_page - $plus;
  259. }
  260. $begin = ($begin >= 1) ? $begin : 1;
  261. $return = '';
  262. $return .= $this->first_page();
  263. $return .= $this->up_page();
  264. for ($i = $begin; $i <= $begin + $plus * 2;$i++)
  265. {
  266. if($i>$this->total_pages)
  267. {
  268. break;
  269. }
  270. if($i == $this->now_page)
  271. {
  272. $return .= "<li class='page-item active'><a class='page-link'>$i</a></li>";
  273. }
  274. else
  275. {
  276. $return .= $this->_get_link($i, $i) . "\n";
  277. }
  278. }
  279. $return .= $this->down_page();
  280. $return .= $this->last_page();
  281. return $return;
  282. }
  283. protected function show_3()
  284. {
  285. $plus = $this->plus;
  286. if( $plus + $this->now_page > $this->total_pages)
  287. {
  288. $begin = $this->total_pages - $plus * 2;
  289. }else{
  290. $begin = $this->now_page - $plus;
  291. }
  292. $begin = ($begin >= 1) ? $begin : 1;
  293. $return = '总计 ' .$this->total_rows. ' 个记录分为 ' .$this->total_pages. ' 页, 当前第 ' . $this->now_page . ' 页 ';
  294. $return .= ',每页 ';
  295. $return .= '<input type="text" value="'.$this->list_rows.'" id="pageSize" size="3"> ';
  296. $return .= $this->first_page()."\n";
  297. $return .= $this->up_page()."\n";
  298. $return .= $this->down_page()."\n";
  299. $return .= $this->last_page()."\n";
  300. $return .= '<select onchange="'.$this->ajax_func_name.'(this.value)" id="gotoPage">';
  301. for ($i = $begin;$i<=$begin+10;$i++)
  302. {
  303. if($i>$this->total_pages)
  304. {
  305. break;
  306. }
  307. if($i == $this->now_page)
  308. {
  309. $return .= '<option selected="true" value="'.$i.'">'.$i.'</option>';
  310. }
  311. else
  312. {
  313. $return .= '<option value="' .$i. '">' .$i. '</option>';
  314. }
  315. }
  316. $return .= '</select>';
  317. return $return;
  318. }
  319. }