PHP Paging class Invoke Instance tutorial

Source: Internet
Author: User
  1. /**

  2. * filename:ext_page.class.php
  3. * @package:p Hpbean
  4. * Description: Ultra-strong paging class, four paging modes, default to a baidu,google-like paging style.
  5. * 2.0 added Features: Support custom style, custom style, support PHP4 and PHP5,
  6. * Example:
  7. * Mode four paging modes:
  8. Require_once ('.. /libs/classes/page.class.php ');
  9. $page =new page (Array (' Total ' =>1000, ' perpage ' =>20));
  10. Echo ' Mode:1
    '. $page->show ();
  11. Echo ' Mode:2
    '. $page->show (2);
  12. Echo ' Mode:3
    '. $page->show (3);
  13. Echo ' Mode:4
    '. $page->show (4);
  14. Open ajax:bbs.it-home.org
  15. $ajaxpage =new page (Array (' Total ' =>1000, ' perpage ' =>20, ' ajax ' = ' ajax_page ', ' page_name ' = ' test '));
  16. Echo ' Mode:1
    '. $ajaxpage->show ();
  17. Adopt the inherited custom paging display mode:
  18. */
  19. Class Page
  20. {
  21. /**
  22. * Config, public
  23. */
  24. var $page _name= "P";//page tag, used to control the URL page. such as xxx.php? The Pb_page in pb_page=2
  25. var $next _page= ' > ';//Next page
  26. var $pre _page= ' < ';//prev
  27. var $first _page= ' first ';//Home
  28. var $last _page= ' last ';//End
  29. var $pre _bar= ' << ';//previous page bar
  30. var $next _bar= ' >> ';//Next page Bar
  31. var $format _left= ';
  32. var $format _right= ';
  33. var $is whether _ajax=false;//supports Ajax paging mode

  34. /**

  35. * Private
  36. *
  37. */
  38. var $pagebarnum =10;//Control the number of record bars.
  39. var $totalpage total Pages =0;//
  40. var $ajax _action_name= ";//ajax action Name
  41. var $nowindex =1;//Current Page
  42. var $url = "";//url address Header
  43. var $offset = 0;

  44. /**

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

  85. }

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

  109. /**

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

  122. /**

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

  134. /**

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

  146. function Nowbar ($style = ", $nowindex _style= ' C ')

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

  178. /**

  179. * Get the value required for limit in MySQL statement
  180. *
  181. * @return String
  182. */
  183. function offset ()
  184. {
  185. return $this->offset;
  186. }

  187. /**

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

  227. }

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

  262. /**

  263. * Set Current page
  264. *
  265. */
  266. function _set_nowindex ($nowindex)
  267. {
  268. if (empty ($nowindex)) {
  269. System acquisition

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

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

  278. /**

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

  288. /**

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

  298. /**

  299. * Get link address
  300. */
  301. function _get_link ($url, $text, $style = ") {
  302. $style = (Empty ($style))? ": ' class=" '. $style. ' ";
  303. if ($this->is_ajax) {
  304. If you are using AJAX mode
  305. Return ' Ajax_action_name. ' (\ '. $url. ') \ ') ' > ' $text. ';
  306. }else{
  307. Return '. $text. ';
  308. }
  309. }
  310. /**
  311. * Error Handling method
  312. */
  313. Function error ($function, $errormsg)
  314. {
  315. Die (' Error in File '. __file__ ' , Function '. $function. ' () : '. $errormsg);
  316. }
  317. }
  318. ?>

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.