Share: Example code for a PHP page (paging) class

Source: Internet
Author: User
  1. /**
  2. * filename:ext_page.class.php
  3. * @package:p Hpbean
  4. * Descrīption: 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. Turn on Ajax:
  15. $ajaxpage =new page (Array (' Total ' =>1000, ' perpage ' =>20, ' ajax ' = ' ajax_page ', ' page_name ' = ' test '));
  16. Echo ' Mode:1
    '. $ajaxpage->show ();
  17. Takes the inherited custom paging display mode.
  18. Editing: Scripting Academy http://bbs.it-home.org
  19. */
  20. Class _page
  21. {
  22. /**
  23. * Config, public
  24. */
  25. var $page _name= "Pb_page";//page tag, used to control the URL page. such as xxx.php? The Pb_page in pb_page=2
  26. var $next _page= ' > ';//Next page
  27. var $pre _page= ' < ';//prev
  28. var $first _page= ' first ';//Home
  29. var $last _page= ' last ';//End
  30. var $pre _bar= ' << ';//previous page bar
  31. var $next _bar= ' >> ';//Next page Bar
  32. var $format _left= ' [';
  33. var $format _right= '];
  34. var $is whether _ajax=false;//supports Ajax paging mode
  35. /**
  36. * Private
  37. *
  38. */
  39. var $pagebarnum =10;//Control the number of record bars.
  40. var $totalpage total Pages =0;//
  41. var $ajax _action_name= ";//ajax action Name
  42. var $nowindex =1;//Current Page
  43. var $url = "";//url address Header
  44. var $offset = 0;
  45. /**
  46. * Constructor Constructor
  47. *
  48. * @param array $array [' total '], $array [' Perpage '], $array [' Nowindex '], $array [' url '], $array [' Ajax '] ...
  49. */
  50. Function page ($array)
  51. {
  52. if (Is_array ($array)) {
  53. if (!array_key_exists (' Total ', $array)) $this->error (__function__, ' need a param of total ');
  54. $total =intval ($array [' total ']);
  55. $perpage = (array_key_exists (' Perpage ', $array))? intval ($array [' Perpage ']): 10;
  56. $nowindex = (array_key_exists (' Nowindex ', $array))? intval ($array [' Nowindex ']): ';
  57. $url = (array_key_exists (' url ', $array))? $array [' url ']: ';
  58. }else{
  59. $total = $array;
  60. $perpage = 10;
  61. $nowindex = ";
  62. $url = ";
  63. }
  64. if ((!is_int ($total)) | | ($total <0)) $this->error (__function__, $total. ' is not a positive integer! ');
  65. if ((!is_int ($perpage)) | | ($perpage <=0)) $this->error (__function__, $perpage. ' is not a positive integer! ');
  66. if (!empty ($array [' page_name '])) $this->set (' page_name ', $array [' page_name ']);//set PAGENAME
  67. $this->_set_nowindex ($nowindex);//Set the current page
  68. $this->_set_url ($url);//Set link address
  69. $this->totalpage=ceil ($total/$perpage);
  70. $this->offset= ($this->nowindex-1) * $perpage;
  71. if (!empty ($array [' Ajax])) $this->open_ajax ($array [' Ajax ']);//Open Ajax mode
  72. }
  73. /**
  74. * Set the value of the variable name specified in the class, if the change does not belong to this class, will throw a exception
  75. *
  76. * @param string $var
  77. * @param string $value
  78. */
  79. function set ($var, $value)
  80. {
  81. if (In_array ($var, Get_object_vars ($this)))
  82. $this $var = $value;
  83. else {
  84. $this->error (__function__, $var. "Does not belong to pb_page!");
  85. }
  86. }
  87. /**
  88. * Turn on inverted Ajax mode
  89. *
  90. * @param string $action The default Ajax-triggered action.
  91. */
  92. function Open_ajax ($action)
  93. {
  94. $this->is_ajax=true;
  95. $this->ajax_action_name= $action;
  96. }
  97. /**
  98. * Get the code that shows "next page"
  99. *
  100. * @param string $style
  101. * @return String
  102. */
  103. function Next_page ($style = ")
  104. {
  105. if ($this->nowindex< $this->totalpage) {
  106. return $this->_get_link ($this->_get_url ($this->nowindex+1), $this->next_page, $style);
  107. }
  108. Return ". $this->next_page.";
  109. }
  110. /**
  111. * Get the code that shows "previous page"
  112. *
  113. * @param string $style
  114. * @return String
  115. */
  116. function Pre_page ($style = ")
  117. {
  118. if ($this->nowindex>1) {
  119. return $this->_get_link ($this->_get_url ($this->nowindex-1), $this->pre_page, $style);
  120. }
  121. Return ". $this->pre_page.";
  122. }
  123. /**
  124. * Get the code that shows "home page"
  125. *
  126. * @return String
  127. */
  128. function First_page ($style = ")
  129. {
  130. if ($this->nowindex==1) {
  131. Return ". $this->first_page.";
  132. }
  133. return $this->_get_link ($this->_get_url (1), $this->first_page, $style);
  134. }
  135. /**
  136. * Get the code that shows "last"
  137. *
  138. * @return String
  139. */
  140. function Last_page ($style = ")
  141. {
  142. if ($this->nowindex== $this->totalpage) {
  143. Return ". $this->last_page.";
  144. }
  145. return $this->_get_link ($this->_get_url ($this->totalpage), $this->last_page, $style);
  146. }
  147. function Nowbar ($style = ", $nowindex _style=")
  148. {
  149. $plus =ceil ($this->PAGEBARNUM/2);
  150. if ($this->pagebarnum-$plus + $this->nowindex> $this->totalpage) $plus = ($this->pagebarnum-$this totalpage+ $this->nowindex);
  151. $begin = $this->nowindex-$plus +1;
  152. $begin = ($begin >=1)? $begin: 1;
  153. $return = ";
  154. for ($i = $begin; $i < $begin + $this->pagebarnum; $i + +)
  155. {
  156. if ($i <= $this->totalpage) {
  157. if ($i! = $this->nowindex)
  158. $return. = $this->_get_text ($this->_get_link ($this->_get_url ($i), $i, $style));
  159. Else
  160. $return. = $this->_get_text (". $i. ');
  161. }else{
  162. Break
  163. }
  164. $return. = "\ n";
  165. }
  166. Unset ($begin);
  167. return $return;
  168. }
  169. /**
  170. * Get the code to show the Jump button
  171. *
  172. * @return String
  173. */
  174. function Select ()
  175. {
  176. $return = ''; for ($i =1; $i <= $this->totalpage; $i + +) {if ($i = = $this->nowindex) {$return. = ''. $i. '; }else{$return. = ''. $i. '; }} unset ($i); $return. = '';
  177. return $return;
  178. }
  179. /**
  180. * Get the value required for limit in MySQL statement
  181. *
  182. * @return String
  183. */
  184. function offset ()
  185. {
  186. return $this->offset;
  187. }
  188. /**
  189. * Control the pagination display style (you can add the corresponding style)
  190. *
  191. * @param int $mode
  192. * @return String
  193. */
  194. Function Show ($mode =1)
  195. {
  196. Switch ($mode)
  197. {
  198. Case ' 1 ':
  199. $this->next_page= ' next page ';
  200. $this->pre_page= ' previous page ';
  201. return $this->pre_page (). $this->nowbar (). $this->next_page (). ' Section '. $this->select (). ' Page ';
  202. Break
  203. Case ' 2 ':
  204. $this->next_page= ' next page ';
  205. $this->pre_page= ' previous page ';
  206. $this->first_page= ' home ';
  207. $this->last_page= ' last ';
  208. return $this->first_page (). $this->pre_page (). ' [No. $this->nowindex] '. $this->next_page (). $this->last_page (). ' Section '. $this->select (). ' Page ';
  209. Break
  210. Case ' 3 ':
  211. $this->next_page= ' next page ';
  212. $this->pre_page= ' previous page ';
  213. $this->first_page= ' home ';
  214. $this->last_page= ' last ';
  215. return $this->first_page (). $this->pre_page (). $this->next_page (). $this->last_page ();
  216. Break
  217. Case ' 4 ':
  218. $this->next_page= ' next page ';
  219. $this->pre_page= ' previous page ';
  220. return $this->pre_page (). $this->nowbar (). $this->next_page ();
  221. Break
  222. Case ' 5 ':
  223. return $this->pre_bar (). $this->pre_page (). $this->nowbar (). $this->next_page (). $this->next_bar ();
  224. Break
  225. Case ' 6 ':
  226. return $this->select ();
  227. Break
  228. Case ' 7 ':
  229. return $this->nowbar ();
  230. Break
  231. }
  232. }
  233. /*----------------Private Function (proprietary method)-----------------------------------------------------------*/
  234. /**
  235. * Set URL header address
  236. * @param: String $url
  237. * @return Boolean
  238. */
  239. function _set_url ($url = "")
  240. {
  241. if (!empty ($url)) {
  242. Manually set
  243. $this->url= $url. (Stristr ($url, '? '))? ' & ': '? '). $this->page_name. " =";
  244. }else{
  245. Automatically get
  246. if (Empty ($_server[' query_string ')) {
  247. When there is no query_string
  248. $this->url=$_server[' Request_uri ']. "?". $this->page_name. " =";
  249. }else{
  250. //
  251. if (Stristr ($_server[' query_string '), $this->page_name. ' =')){
  252. Address exists page parameter
  253. $this->url=str_replace ($this->page_name. ' = '. $this->nowindex, ' ', $_server[' Request_uri ']);
  254. $last = $this->url[strlen ($this->url)-1];
  255. if ($last = = '? ' | | $last = = ' & ') {
  256. $this->url.= $this->page_name. " =";
  257. }else{
  258. $this->url.= ' & $this->page_name. " =";
  259. }
  260. }else{
  261. //
  262. $this->url=$_server[' Request_uri ']. ' & '. $this->page_name. ' =';
  263. }//end if
  264. }//end if
  265. }//end if
  266. }
  267. /**
  268. * Set Current page
  269. *
  270. */
  271. function _set_nowindex ($nowindex)
  272. {
  273. if (empty ($nowindex)) {
  274. System acquisition
  275. if (Isset ($_get[$this->page_name])) {
  276. $this->nowindex=intval ($_get[$this->page_name]);
  277. }
  278. if (Isset ($_post[' Pb_page_select ')) {
  279. $this->nowindex=$_post[' Pb_page_select '];
  280. }
  281. }else{
  282. Manually set
  283. $this->nowindex=intval ($nowindex);
  284. }
  285. }
  286. /**
  287. * Returns the address value for the specified page
  288. *
  289. * @param int $pageno
  290. * @return String $url
  291. */
  292. function _get_url ($pageno =1)
  293. {
  294. Return $this->url. $pageno;
  295. }
  296. /**
  297. * Get pagination display text, e.g. _get_text (' 1 ') will return by default [1]
  298. *
  299. * @param String $str
  300. * @return String $url
  301. */
  302. function _get_text ($STR)
  303. {
  304. Return $this->format_left. $str. $this->format_right;
  305. }
  306. /**
  307. * Get link address
  308. */
  309. function _get_link ($url, $text, $style = ") {
  310. $style = (Empty ($style))? ": ' class=" '. $style. ' ";
  311. if ($this->is_ajax) {
  312. If you are using AJAX mode
  313. Return ' Ajax_action_name. ' (\ '. $url. ') \ ') ' > ' $text. ';
  314. }else{
  315. Return '. $text. ';
  316. }
  317. }
  318. /**
  319. * Error Handling method
  320. */
  321. Function error ($function, $errormsg)
  322. {
  323. Die (' Error in File '. __file__ ' , Function '. $function. ' () : '. $errormsg);
  324. }
  325. }
  326. Inherit the paging class and join the database access capability.
  327. Class Page extends _page {
  328. var $db; DB Connected Object
  329. var $_sql_query = '; Querying the SQL for a database
  330. var $_total = 0; The total record that was queried. Must first
  331. var $_rst = array (); The record that was queried.
  332. /**
  333. * Paged Query class library.
  334. *
  335. * @param String $SQL The SQL statement that records the query.
  336. * @param int $pagenuber How many records per page.
  337. * @param int $pagen the current page.
  338. * @param String $url The parameters that are included in the paging link. Index.php?xx=b&bb=33
  339. * @param String $pname The current page of markup, default is index.php?xx=b&bb=33&page=2 if there are special requirements
  340. You can modify the parameters of the $pname. For example: $pname = ' db_page ', it becomes: index.php?xx=b&bb=33&db_page=2
  341. * @return Mysql_page
  342. */
  343. function Page ($db, $sql _query = ", $max _rows_per_page = $, $current _page_number = 0, $url = ', $parameters = ', $pname = ' Pb_page ', $OTC = ' * ') {
  344. $this db = $db;
  345. $pos _to = strlen ($sql _query);
  346. $pos _from = Strpos ($sql _query, ' from ', 0);
  347. $pos _group_by = Strpos ($sql _query, ' GROUP by ', $pos _from);
  348. if ($pos _group_by < $pos _to) && ($pos _group_by! = False)) $pos _to = $pos _group_by;
  349. $pos _having = Strpos ($sql _query, ' having ', $pos _from);
  350. if ($pos _having < $pos _to) && ($pos _having! = False)) $pos _to = $pos _having;
  351. $pos _order_by = Strpos ($sql _query, ' ORDER by ', $pos _from);
  352. if ($pos _order_by < $pos _to) && ($pos _order_by! = False)) $pos _to = $pos _order_by;
  353. $reviews _count = $this, db, GetResults ("SELECT count ($OTC) as Total". substr ($sql _query, $pos _from, ($pos _to-$pos _from));
  354. $query _num_rows = $reviews _count[0][' total ');
  355. $this-_total = $query _num_rows;
  356. $num _pages = ceil ($query _num_rows/$max _rows_per_page);
  357. if ($current _page_number > $num _pages) {
  358. $current _page_number = $num _pages;
  359. }
  360. $offset = ($max _rows_per_page * ($current _page_number-1));
  361. if ($offset < 0) $offset = 0;
  362. if ($offset > 0) {
  363. $offset = $offset + 1;
  364. }
  365. $this-_sql_query = $sql _query. "Limit". $offset. ", " . $max _rows_per_page;
  366. $this-SetData (); Query the database.
  367. Parent:: page (' total ' = $query _num_rows, ' perpage ' = = $max _rows_per_page, ' page_name ' + = $pname, ' url ' =& Gt $url, ' parameters ' = $parameters));
  368. }
  369. /**
  370. * Gets a record of the current page, returning an array.
  371. */
  372. function Findbyall () {
  373. return $this, _rst;
  374. }
  375. /**
  376. * Display paging information.
  377. *
  378. * @param int $model
  379. */
  380. function Dispaly_links ($model) {
  381. $this, Show ($model);
  382. }
  383. /**
  384. * Returns the number of records.
  385. *
  386. * @return Int
  387. */
  388. function GetCount () {
  389. return $this, _total;
  390. }
  391. /**
  392. * Fetch query result record number.
  393. *
  394. * @return Int
  395. */
  396. function GetRows () {
  397. Return count ($this-_rst);
  398. }
  399. /**
  400. * Perform query function.
  401. * Computes an array.
  402. * Private method.
  403. */
  404. function SetData () {
  405. $this-_rst = $this, db, GetResults ($this, _sql_query);
  406. }
  407. }
  408. ?>
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.