A nice code for the PHP paging class

Source: Internet
Author: User
  1. /**

  2. * filename:ext_page.class.php
  3. * @package:p Hpbean
  4. * @author: FEIFENGXLQ
  5. * @copyright: Copyright 2006 FEIFENGXLQ
  6. * @license: Version 2.0
  7. * Description: Paging class, four paging modes
  8. * 2.0 added Features: Support custom style, custom style, support PHP4 and PHP5,
  9. * To see Detail,please visit http://www.phpobject.net/blog/read.php?
  10. * Example:
  11. * Mode four paging modes:
  12. Require_once ('.. /libs/classes/page.class.php ');
  13. $page =new page (Array (' Total ' =>1000, ' perpage ' =>20));
  14. Echo ' Mode:1
    '. $page->show ();
  15. Echo ' Mode:2
    '. $page->show (2);
  16. Echo ' Mode:3
    '. $page->show (3);
  17. Echo ' Mode:4
    '. $page->show (4);
  18. Turn on Ajax:
  19. $ajaxpage =new page (Array (' Total ' =>1000, ' perpage ' =>20, ' ajax ' = ' ajax_page ', ' page_name ' = ' test '));
  20. Echo ' Mode:1
    '. $ajaxpage->show ();
  21. Adopt the inherited custom paging display mode:
  22. Demo:http://www.phpobject.net/blog
  23. */
  24. Class page
  25. {
  26. /**
  27. * Config, public
  28. */
  29. var $page _name= "Pb_page";//page tag, used to control the URL page
  30. var $next _page= ' > ';//Next page
  31. var $pre _page= ' < ';//prev
  32. var $first _page= ' first ';//Home
  33. var $last _page= ' last ';//End
  34. var $pre _bar= ' << ';//previous page bar
  35. var $next _bar= ' >> ';//Next page Bar
  36. var $format _left= ' [';
  37. var $format _right= '];
  38. var $is whether _ajax=false;//supports Ajax paging mode

  39. /**

  40. * Private
  41. *
  42. */
  43. var $pagebarnum =10;//Control the number of record bars.
  44. var $totalpage total Pages =0;//
  45. var $ajax _action_name= ";//ajax action Name
  46. var $nowindex =1;//Current Page
  47. var $url = "";//url address Header
  48. var $offset = 0;

  49. /**

  50. * Constructor Constructor
  51. *
  52. * @param array $array [' total '], $array [' Perpage '], $array [' Nowindex '], $array [' url '], $array [' Ajax ']
  53. */
  54. Function page ($array)
  55. {
  56. if (Is_array ($array)) {
  57. if (!array_key_exists (' Total ', $array)) $this->error (__function__, ' need a param of total ');
  58. $total =intval ($array [' total ']);
  59. $perpage = (array_key_exists (' Perpage ', $array))? intval ($array [' Perpage ']): 10;
  60. $nowindex = (array_key_exists (' Nowindex ', $array))? intval ($array [' Nowindex ']): ';
  61. $url = (array_key_exists (' url ', $array))? $array [' url ']: ';
  62. }else{
  63. $total = $array;
  64. $perpage = 10;
  65. $nowindex = ";
  66. $url = ";
  67. }
  68. if ((!is_int ($total)) | | ($total <0)) $this->error (__function__, $total. ' is not a positive integer! ');
  69. if ((!is_int ($perpage)) | | ($perpage <=0)) $this->error (__function__, $perpage. ' is not a positive integer! ');
  70. if (!empty ($array [' page_name '])) $this->set (' page_name ', $array [' page_name ']);//set PAGENAME
  71. $this->_set_nowindex ($nowindex);//Set the current page
  72. $this->_set_url ($url);//Set link address
  73. $this->totalpage=ceil ($total/$perpage);
  74. $this->offset= ($this->nowindex-1) * $perpage;
  75. if (!empty ($array [' Ajax])) $this->open_ajax ($array [' Ajax ']);//Open Ajax mode
  76. }
  77. /**
  78. * Set the value of the variable name specified in the class, if the change does not belong to this class, will throw a exception
  79. *
  80. * @param string $var
  81. * @param string $value
  82. */
  83. function set ($var, $value)
  84. {
  85. if (In_array ($var, Get_object_vars ($this)))
  86. $this $var = $value;
  87. else {
  88. $this->error (__function__, $var. "Does not belong to pb_page!");
  89. }

  90. }

  91. /**
  92. * Turn on inverted Ajax mode
  93. *
  94. * @param string $action The default Ajax-triggered action.
  95. */
  96. function Open_ajax ($action)
  97. {
  98. $this->is_ajax=true;
  99. $this->ajax_action_name= $action;
  100. }
  101. /**
  102. * Get the code that shows "next page"
  103. *
  104. * @param string $style
  105. * @return String
  106. */
  107. function Next_page ($style = ")
  108. {
  109. if ($this->nowindex< $this->totalpage) {
  110. return $this->_get_link ($this->_get_url ($this->nowindex+1), $this->next_page, $style);
  111. }
  112. Return ". $this->next_page.";
  113. }

  114. /**

  115. * Get the code that shows "previous page"
  116. *
  117. * @param string $style
  118. * @return String
  119. */
  120. function Pre_page ($style = ")
  121. {
  122. if ($this->nowindex>1) {
  123. return $this->_get_link ($this->_get_url ($this->nowindex-1), $this->pre_page, $style);
  124. }
  125. Return ". $this->pre_page.";
  126. }

  127. /**

  128. * Get the code that shows "home page"
  129. *
  130. * @return String
  131. */
  132. function First_page ($style = ")
  133. {
  134. if ($this->nowindex==1) {
  135. Return ". $this->first_page.";
  136. }
  137. return $this->_get_link ($this->_get_url (1), $this->first_page, $style);
  138. }

  139. /**

  140. * Get the code that shows "last"
  141. *
  142. * @return String
  143. */
  144. function Last_page ($style = ")
  145. {
  146. if ($this->nowindex== $this->totalpage) {
  147. Return ". $this->last_page.";
  148. }
  149. return $this->_get_link ($this->_get_url ($this->totalpage), $this->last_page, $style);
  150. }

  151. function Nowbar ($style = ", $nowindex _style=")

  152. {
  153. $plus =ceil ($this->PAGEBARNUM/2);
  154. if ($this->pagebarnum-$plus + $this->nowindex> $this->totalpage) $plus = ($this->pagebarnum-$this totalpage+ $this->nowindex);
  155. $begin = $this->nowindex-$plus +1;
  156. $begin = ($begin >=1)? $begin: 1;
  157. $return = ";
  158. for ($i = $begin; $i < $begin + $this->pagebarnum; $i + +)
  159. {
  160. if ($i <= $this->totalpage) {
  161. if ($i! = $this->nowindex)
  162. $return. = $this->_get_text ($this->_get_link ($this->_get_url ($i), $i, $style));
  163. Else
  164. $return. = $this->_get_text (". $i. ');
  165. }else{
  166. Break
  167. }
  168. $return. = "\ n";
  169. }
  170. Unset ($begin);
  171. return $return;
  172. }
  173. /**
  174. * Get the code to show the Jump button
  175. *
  176. * @return String
  177. */
  178. function Select ()
  179. {
  180. $return = ''; for ($i =1; $i <= $this->totalpage; $i + +) {if ($i = = $this->nowindex) {$return. = ''. $i. '; }else{$return. = ''. $i. '; }} unset ($i); $return. = '';
  181. return $return;
  182. }

  183. /**

  184. * Get the value required for limit in MySQL statement
  185. *
  186. * @return String
  187. */
  188. function offset ()
  189. {
  190. return $this->offset;
  191. }

  192. /**

  193. * Control pagination Display style
  194. *
  195. * @param int $mode
  196. * @return String
  197. */
  198. Function Show ($mode =1)
  199. {
  200. Switch ($mode)
  201. {
  202. Case ' 1 ':
  203. $this->next_page= ' next page ';
  204. $this->pre_page= ' previous page ';
  205. return $this->pre_page (). $this->nowbar (). $this->next_page (). ' Section '. $this->select (). ' Page ';
  206. Break
  207. Case ' 2 ':
  208. $this->next_page= ' next page ';
  209. $this->pre_page= ' previous page ';
  210. $this->first_page= ' home ';
  211. $this->last_page= ' last ';
  212. return $this->first_page (). $this->pre_page (). ' [No. $this->nowindex] '. $this->next_page (). $this->last_page (). ' Section '. $this->select (). ' Page ';
  213. Break
  214. Case ' 3 ':
  215. $this->next_page= ' next page ';
  216. $this->pre_page= ' previous page ';
  217. $this->first_page= ' home ';
  218. $this->last_page= ' last ';
  219. return $this->first_page (). $this->pre_page (). $this->next_page (). $this->last_page ();
  220. Break
  221. Case ' 4 ':
  222. $this->next_page= ' next page ';
  223. $this->pre_page= ' previous page ';
  224. return $this->pre_page (). $this->nowbar (). $this->next_page ();
  225. Break
  226. Case ' 5 ':
  227. return $this->pre_bar (). $this->pre_page (). $this->nowbar (). $this->next_page (). $this->next_bar ();
  228. Break
  229. }

  230. }

  231. /*------Private Function (proprietary method)---------*/
  232. /** @link: http://bbs.it-home.org
  233. * Set URL header address
  234. * @param: String $url
  235. * @return Boolean
  236. */
  237. function _set_url ($url = "")
  238. {
  239. if (!empty ($url)) {
  240. Manually set
  241. $this->url= $url. (Stristr ($url, '? '))? ' & ': '? '). $this->page_name. " =";
  242. }else{
  243. Automatically get
  244. if (Empty ($_server[' query_string ')) {
  245. When there is no query_string
  246. $this->url=$_server[' Request_uri ']. "?". $this->page_name. " =";
  247. }else{
  248. //
  249. if (Stristr ($_server[' query_string '), $this->page_name. ' =')){
  250. Address exists page parameter
  251. $this->url=str_replace ($this->page_name. ' = '. $this->nowindex, ' ', $_server[' Request_uri ']);
  252. $last = $this->url[strlen ($this->url)-1];
  253. if ($last = = '? ' | | $last = = ' & ') {
  254. $this->url.= $this->page_name. " =";
  255. }else{
  256. $this->url.= ' & $this->page_name. " =";
  257. }
  258. }else{
  259. //
  260. $this->url=$_server[' Request_uri ']. ' & '. $this->page_name. ' =';
  261. }//end if
  262. }//end if
  263. }//end if
  264. }

  265. /**

  266. * Set Current page
  267. *
  268. */
  269. function _set_nowindex ($nowindex)
  270. {
  271. if (empty ($nowindex)) {
  272. System acquisition

  273. if (Isset ($_get[$this->page_name])) {

  274. $this->nowindex=intval ($_get[$this->page_name]);
  275. }
  276. }else{
  277. Manually set
  278. $this->nowindex=intval ($nowindex);
  279. }
  280. }

  281. /**

  282. * Returns the address value for the specified page
  283. *
  284. * @param int $pageno
  285. * @return String $url
  286. */
  287. function _get_url ($pageno =1)
  288. {
  289. Return $this->url. $pageno;
  290. }

  291. /**

  292. * Get pagination display text, e.g. _get_text (' 1 ') will return by default [1]
  293. *
  294. * @param String $str
  295. * @return String $url
  296. */
  297. function _get_text ($STR)
  298. {
  299. Return $this->format_left. $str. $this->format_right;
  300. }

  301. /**

  302. * Get link address
  303. */
  304. function _get_link ($url, $text, $style = ") {
  305. $style = (Empty ($style))? ": ' class=" '. $style. ' ";
  306. if ($this->is_ajax) {
  307. If you are using AJAX mode
  308. Return ' Ajax_action_name. ' (\ '. $url. ') \ ') ' > ' $text. ';
  309. }else{
  310. Return '. $text. ';
  311. }
  312. }
  313. /**
  314. * Error Handling method
  315. */
  316. Function error ($function, $errormsg)
  317. {
  318. Die (' Error in File '. __file__ ' , Function '. $function. ' () : '. $errormsg);
  319. }
  320. }
  321. $page =new page (Array (' Total ' =>1000, ' perpage ' =>20));
  322. Echo ' Mode:1
    '. $page->show ();
  323. Echo ' Mode:2
    '. $page->show (2);
  324. Echo ' Mode:3
    '. $page->show (3);
  325. Echo ' Mode:4
    '. $page->show (4);
  326. Echo ' Start Ajax mode: ';
  327. $ajaxpage =new page (Array (' Total ' =>1000, ' perpage ' =>20, ' ajax ' = ' ajax_page ', ' page_name ' = ' test '));
  328. Echo ' Mode:1
    '. $ajaxpage->show ();
  329. ?>

Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.