首頁  >  文章  >  後端開發  >  封裝好的PHP分頁類,簡單好用

封裝好的PHP分頁類,簡單好用

WBOY
WBOY原創
2016-07-25 09:06:38983瀏覽
{ echo '';
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 _ = 0;
  14. 受保護的$_page = 1;
  15. 受保護的$_page_size = 10;
  16. 受保護的$_link = '';
  17. 受保護的$_grep = 3;
  18. 受保護的$_admin = false;
  19. protected $_css_next = '下一頁';
  20. protected $_css_prev = '上一頁';
  21. protected $_css_curr = '當前頁';
  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. if ( $ > $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. 繼續;
  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. { $ ;_page_size = 10;
  82. }
  83. else
  84. {
  85. $this->_page_size = (int) $page_size;
  86. }
  87. }
  88. }
  89. }
  90. {
  91. $page_size = 空( $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 ( 空( $page ) )
  109. { $_page = 1;
  110. }
  111. else
  112. {
  113. $this-> _page = (int) $page;
  114. }
  115. }
  116. public function get
  117. }
  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 { return '
  135. 下一頁
  136. '; } }
  137. if ( $this->_admin )
  138. return '下一頁?';
  139. else
  140. return '';
  141. }
  142. public function get_prev_page_btn ( ) {
  143. if ( $this->_page > 1 )
  144. {
  145. $link = '';
  146. if ( strpos( $this->_link, '?' ) )
  147. {
  148. $link = $this->_link.'&page='.( $this->_page - 1 );
  149. }
  150. else
  151. {
  152. $link = $this->_link .'?page='.( $this->_page - 1 );
  153. }
  154. if ( $this->_admin )
  155. {
  156. return '上一頁';
  157. }
  158. else
  159. {
  160. return '
  161. 上一頁
  162. ';
  163. }
  164. }
  165. if ( $this->_admin )
  166. return '? 上一頁';
  167. else
  168. return '';
  169. }
  170. public function get_current_page ()
  171. {
  172. if ( $this->_admin )
  173. {
  174. if ( $this->_admin )
  175. return ''.$this->_page.'';
  176. else
  177. return '
  178. '.$this->_page .'
  179. ';
  180. }
  181. 公用函數get_page_link ( $page )
  182. {
  183. $link = '';
  184. if ( strpos ( $this->_link, '?' ) )
  185. {
  186. $link = $this->_link.'&page='. $page;
  187. }
  188. else
  189. {
  190. $link = $this->_link.'?page='.$page;
  191. }
  192. if ( $this->_admin )
  193. {
  194. return ''.$page.'';
  195. }
  196. else
  197. {
  198. return '
  199. '.$page.'
  200. ';
  201. }
  202. }
  203. 公用函數get_prev_pages ()
  204. {
  205. $pages = array();
  206. $begin = $this->_page - $this->_grep;
  207. if ( $begin {
  208. $begin = 1;
  209. }
  210. elseif ( $begin > 2 )
  211. {
  212. $pages[] = $this->get_page_link( 1 ) ;
  213. if ( $this->_admin )
  214. {
  215. $pages[] = ' ... ';
  216. }
  217. else
  218. {
  219. $ page[] = '> else
  220. {
  221. $ page[] = '
  222. ...
  223. ';
  224. }
  225. }
  226. elseif ( $begin == 2 )
  227. {
  228. $pages[] = $this->get_page_link( 1 );
  229. }
  230. for ( $i = $begin; $i _page; $i++ )
  231. {
  232. $pages[] = $this->get_page_link( $i ) ;
  233. }
  234. return $pages;
  235. }
  236. public function get_next_pages ()
  237. {
  238. $pages = array( );
  239. $be = $-me >_page + 1;
  240. if ( $begin _total_page )
  241. {
  242. $end = $begin + $this-> ;_grep;
  243. if ( $end > $this- >_total_page )
  244. {
  245. $end = $this->_total_page;
  246. }
  247. for ( $i = $begin ; $i {
  248. $pages[] = $this->get_page_link( $i );
  249. }
  250. if ( $i _total_page )
  251. {
  252. if ( $this->_admin )
  253. {
  254. $pages[] = ' ... ';
  255. }
  256. else
  257. {
  258. $pages[] = '
  259. ...
  260. ';
  261. }
  262. $pages[] = $this->get_page_link( $ this->_total_page );
  263. }
  264. else
  265. {
  266. $pages[] = $this->get_page_link->get_page_link ( $this->_total_page );
  267. }
  268. }
  269. elseif ( $begin == $this->_total_page )
  270. {
  271. $pages[] = $this->get_page_link( $ this->_total_page );
  272. }
  273. return $pages;
  274. }
  275. public function show ()
  276. {
  277. if ( $this->_total_page { 回傳; }
  278. if ( $this->_admin )
  279. {
  280. echo '

    ';

  281. echo '共同'.$this->; _total.'筆記錄';
  282. }
  283. else
  284. {
  285. echo '
      ';
    • }
    • echo $this->get_prev_page_btn();
    • $prev_pages = $this->get_prev_pages ();
    • if ( !empty( $prev_pages ) );
    • if ( !empty( $prev_pages ) ) {
    • echo $page;
    • }
    • }
    • echo $this->get_current_page();
    • $next_pages = $this->get_next_page ();
    • $next_pages = $this->get_next_page ( );
    • if ( !empty( $next_pages ) )
    • {
    • foreach ( $next_pages as $page )
    • {
    • echo $page;
    • }
    • }
    • echo $page;
    • }
    • }
    • echo $page;
    • }
    • }
    • echo $page;
    • }
    • }
    • echo $this->get_next_page_btn();
    • if ( $this->_admin )
{
echo ''; } else
{
} }
}
複製程式碼

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn