Heim > Backend-Entwicklung > PHP-Tutorial > 封装好的PHP分页类,简单好用

封装好的PHP分页类,简单好用

WBOY
Freigeben: 2016-07-25 09:06:38
Original
1017 Leute haben es durchsucht
class Pagination 独立分页类
调用方式:
$pagenation = new Pagination( 4, 10, 200 ); // 4(第一个参数) = currentPage, 10(第二个参数) = pageSize, 200(第三个参数) = 总数
$pagenation->set_link( 'http://www.360hqb.com' );
$pagenation->show();

  1. /**
  2. * class Pagination
  3. *
  4. * 独立分页类
  5. * 调用方式:
  6. * $pagenation = new Pagination( 4, 10, 200 ); // 4(第一个参数) = currentPage, 10(第二个参数) = pageSize, 200(第三个参数) = 总数
  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 _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 {
  211. $begin = 1;
  212. }
  213. elseif ( $begin > 2 )
  214. {
  215. $pages[] = $this->get_page_link( 1 );
  216. if ( $this->_admin )
  217. {
  218. $pages[] = ' ... ';
  219. }
  220. else
  221. {
  222. $pages[] = '
  223. ...
  224. ';
  225. }
  226. }
  227. elseif ( $begin == 2 )
  228. {
  229. $pages[] = $this->get_page_link( 1 );
  230. }
  231. for ( $i = $begin; $i _page; $i++ )
  232. {
  233. $pages[] = $this->get_page_link( $i );
  234. }
  235. return $pages;
  236. }
  237. public function get_next_pages ()
  238. {
  239. $pages = array();
  240. $begin = $this->_page + 1;
  241. if ( $begin _total_page )
  242. {
  243. $end = $begin + $this->_grep;
  244. if ( $end > $this->_total_page )
  245. {
  246. $end = $this->_total_page;
  247. }
  248. for ( $i = $begin; $i {
  249. $pages[] = $this->get_page_link( $i );
  250. }
  251. if ( $i _total_page )
  252. {
  253. if ( $this->_admin )
  254. {
  255. $pages[] = ' ... ';
  256. }
  257. else
  258. {
  259. $pages[] = '
  260. ...
  261. ';
  262. }
  263. $pages[] = $this->get_page_link( $this->_total_page );
  264. }
  265. else
  266. {
  267. $pages[] = $this->get_page_link( $this->_total_page );
  268. }
  269. }
  270. elseif ( $begin == $this->_total_page )
  271. {
  272. $pages[] = $this->get_page_link( $this->_total_page );
  273. }
  274. return $pages;
  275. }
  276. public function show ()
  277. {
  278. if ( $this->_total_page {
  279. return;
  280. }
  281. if ( $this->_admin )
  282. {
  283. echo '

    ';

  284. echo '共有'.$this->_total.'条记录';
  285. }
  286. else
  287. {
  288. 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 '
    ';
  289. }
  290. }
  291. }
复制代码


Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage