Home  >  Article  >  Backend Development  >  Encapsulated PHP paging class, simple and easy to use

Encapsulated PHP paging class, simple and easy to use

WBOY
WBOYOriginal
2016-07-25 09:06:381007browse
class Pagination independent paging class
Calling method:
$pagenation = new Pagination( 4, 10, 200 ); // 4(first parameter) = currentPage, 10(second parameter) = pageSize, 200(third parameter) = total number
$pagenation->set_link( 'http://www.360hqb.com' );
$pagenation->show();

  1. /**
  2. * class Pagination
  3. *
  4. * Independent pagination class
  5. * Calling method:
  6. * $pagenation = new Pagination( 4, 10, 200 ); // 4 (first parameter) = currentPage, 10 (second parameter) = pageSize, 200 (third parameter) = total number
  7. * $pagenation->set_link( 'http://www.360hqb.com' );
  8. * $pagenation->show();
  9. */
  10. class Java_Pagination
  11. {
  12. protected $_total = 0;
  13. protected $_total_page = 0;
  14. protected $_page = 1;
  15. protected $_page_size = 10;
  16. protected $_link = '';
  17. protected $_grep = 3;
  18. protected $_admin = false;
  19. protected $_css_next = 'next-page';
  20. protected $_css_prev = 'prev-page';
  21. protected $_css_curr = 'curr-page';
  22. protected $_css_page = 'page-nav inline-block';
  23. public function __construct ( $page, $page_size, $total, $admin = false )
  24. {
  25. $this->set_current_page( $page );
  26. $this->set_page_size( $page_size );
  27. $this->set_total( $total );
  28. if ( $admin )
  29. {
  30. $this->_admin = $admin;
  31. }
  32. //$this->_link = $_SERVER['REQUEST_URI'];
  33. $this->set_link( $_SERVER['REQUEST_URI'] );
  34. }
  35. public function set_link ( $link, $is_shop = TRUE )
  36. {
  37. $len = strlen( $link );
  38. $substr = substr( $link, $len - 1 );
  39. if ( '&' == $substr )
  40. {
  41. $link = substr( $link, 0, $len - 1 );
  42. }
  43. $pos = strpos( $link, '?' );
  44. if ( $pos )
  45. {
  46. $link = substr( $link, 0, $pos );
  47. }
  48. if ( !empty( $_GET ) )
  49. {
  50. $link .= '?';
  51. foreach ( $_GET as $k=>$v )
  52. {
  53. if ( 'page' == strtolower( $k ) )
  54. {
  55. continue;
  56. }
  57. $link .= $k.'='.$v.'&';
  58. }
  59. $len = strlen( $link );
  60. $substr = substr( $link, $len - 1 );
  61. if ( '&' == $substr )
  62. {
  63. $link = substr( $link, 0, $len - 1);
  64. }
  65. }
  66. elseif ( isset( $_SERVER['QUERY_STRING'] ) AND !empty( $_SERVER['QUERY_STRING'] ) AND $is_shop )
  67. {
  68. $link .= '?'.$_SERVER['QUERY_STRING'];
  69. $len = strlen( $link );
  70. $substr = substr( $link, $len - 1 );
  71. if ( '&' == $substr )
  72. {
  73. $link = substr( $link, 0, $len - 1);
  74. }
  75. }
  76. $this->_link = $link;
  77. }
  78. public function set_page_size ( $page_size )
  79. {
  80. if ( empty( $page_size ) )
  81. {
  82. $this->_page_size = 10;
  83. }
  84. else
  85. {
  86. $this->_page_size = (int) $page_size;
  87. }
  88. }
  89. public function set_total ( $total )
  90. {
  91. $page_size = empty( $this->_page_size )?10:$this->_page_size;
  92. $this->_total = $total;
  93. if ( 0 == ( $total % $page_size ) )
  94. {
  95. $this->_total_page = intval( $total / $page_size );
  96. }
  97. else
  98. {
  99. $this->_total_page = intval( $total / $page_size ) + 1;
  100. }
  101. if ( $this->_page > $this->_total_page )
  102. {
  103. $this->_page = $this->_total_page;
  104. }
  105. }
  106. public function set_current_page ( $page )
  107. {
  108. if ( empty( $page ) )
  109. {
  110. $this->_page = 1;
  111. }
  112. else
  113. {
  114. $this->_page = (int) $page;
  115. }
  116. }
  117. public function get_next_page_btn ()
  118. {
  119. if ( $this->_page < $this->_total_page )
  120. {
  121. $link = '';
  122. if ( strpos( $this->_link, '?' ) )
  123. {
  124. $link = $this->_link.'&page='.( $this->_page + 1 );
  125. }
  126. else
  127. {
  128. $link = $this->_link.'?page='.( $this->_page + 1 );
  129. }
  130. if ( $this->_admin )
  131. {
  132. return '下一页';
  133. }
  134. else
  135. {
  136. return '
  137. 下一页
  138. ';
  139. }
  140. }
  141. if ( $this->_admin )
  142. return '下一页 ?';
  143. else
  144. return '';
  145. }
  146. public function get_prev_page_btn ()
  147. {
  148. if ( $this->_page > 1 )
  149. {
  150. $link = '';
  151. if ( strpos( $this->_link, '?' ) )
  152. {
  153. $link = $this->_link.'&page='.( $this->_page - 1 );
  154. }
  155. else
  156. {
  157. $link = $this->_link.'?page='.( $this->_page - 1 );
  158. }
  159. if ( $this->_admin )
  160. {
  161. return '上一页';
  162. }
  163. else
  164. {
  165. return '
  166. 上一页
  167. ';
  168. }
  169. }
  170. if ( $this->_admin )
  171. return '? 上一页';
  172. else
  173. return '';
  174. }
  175. public function get_current_page ()
  176. {
  177. if ( $this->_admin )
  178. return ''.$this->_page.'';
  179. else
  180. return '
  181. '.$this->_page.'
  182. ';
  183. }
  184. public function get_page_link ( $page )
  185. {
  186. $link = '';
  187. if ( strpos( $this->_link, '?' ) )
  188. {
  189. $link = $this->_link.'&page='.$page;
  190. }
  191. else
  192. {
  193. $link = $this->_link.'?page='.$page;
  194. }
  195. if ( $this->_admin )
  196. {
  197. return ''.$page.'';
  198. }
  199. else
  200. {
  201. return '
  202. '.$page.'
  203. ';
  204. }
  205. }
  206. public function get_prev_pages ()
  207. {
  208. $pages = array();
  209. $begin = $this->_page - $this->_grep;
  210. if ( $begin < 1 )
  211. {
  212. $begin = 1;
  213. }
  214. elseif ( $begin > 2 )
  215. {
  216. $pages[] = $this->get_page_link( 1 );
  217. if ( $this->_admin )
  218. {
  219. $pages[] = ' ... ';
  220. }
  221. else
  222. {
  223. $pages[] = '
  224. ...
  225. ';
  226. }
  227. }
  228. elseif ( $begin == 2 )
  229. {
  230. $pages[] = $this->get_page_link( 1 );
  231. }
  232. for ( $i = $begin; $i < $this->_page; $i++ )
  233. {
  234. $pages[] = $this->get_page_link( $i );
  235. }
  236. return $pages;
  237. }
  238. public function get_next_pages ()
  239. {
  240. $pages = array();
  241. $begin = $this->_page + 1;
  242. if ( $begin < $this->_total_page )
  243. {
  244. $end = $begin + $this->_grep;
  245. if ( $end > $this->_total_page )
  246. {
  247. $end = $this->_total_page;
  248. }
  249. for ( $i = $begin; $i < $end; $i++ )
  250. {
  251. $pages[] = $this->get_page_link( $i );
  252. }
  253. if ( $i < $this->_total_page )
  254. {
  255. if ( $this->_admin )
  256. {
  257. $pages[] = ' ... ';
  258. }
  259. else
  260. {
  261. $pages[] = '
  262. ...
  263. ';
  264. }
  265. $pages[] = $this->get_page_link( $this->_total_page );
  266. }
  267. else
  268. {
  269. $pages[] = $this->get_page_link( $this->_total_page );
  270. }
  271. }
  272. elseif ( $begin == $this->_total_page )
  273. {
  274. $pages[] = $this->get_page_link( $this->_total_page );
  275. }
  276. return $pages;
  277. }
  278. public function show ()
  279. {
  280. if ( $this->_total_page <= 1 )
  281. {
  282. return;
  283. }
  284. if ( $this->_admin )
  285. {
  286. echo '

    ';

  287. echo '共有'.$this->_total.'条记录';
  288. }
  289. else
  290. {
  291. echo '
      ';
    • }
    • echo $this->get_prev_page_btn();
    • $prev_pages = $this->get_prev_pages ();
    • if ( !empty( $prev_pages ) )
    • {
    • foreach ( $prev_pages as $page )
    • {
    • echo $page;
    • }
    • }
    • echo $this->get_current_page();
    • $next_pages = $this->get_next_pages ();
    • if ( !empty( $next_pages ) )
    • {
    • foreach ( $next_pages as $page )
    • {
    • echo $page;
    • }
    • }
    • echo $this->get_next_page_btn();
    • if ( $this->_admin )
    • {
    • echo '

      ';
    • }
    • else
    • {
    • echo '
    ';
  292. }
  293. }
  294. }
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn