Share: an example of PHP paging instance code

Source: Internet
Author: User
Share: an example of PHP paging instance code

  1. /**
  2. * Filename: ext_page.class.php
  3. * @ Package: phpbean
  4. * Descr paging ption: powerful paging mode. it adopts four paging modes, which are similar to baidu and google by default.
  5. * 2.0 added: supports custom styles and styles, and supports both 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. Enable AJAX:
  15. $ Ajaxpage = new page (array ('total' => 1000, 'perpage' => 20, 'Ajax '=> 'Ajax _ page ', 'page _ name' => 'test '));
  16. Echo 'MoDe: 1
    '. $ Ajaxpage-> show ();
  17. Use the inherited custom paging display mode.
  18. Edit: Script school http://bbs.it-home.org
  19. */
  20. Class _ page
  21. {
  22. /**
  23. * Config, public
  24. */
  25. Var $ page_name = "PB_page"; // page label, used to control url pages. For example, xxx. php? PB_page = PB_page in 2
  26. Var $ next_page = '>'; // Next page
  27. Var $ pre_page = '<'; // Previous Page
  28. Var $ first_page = 'first'; // home page
  29. Var $ last_page = 'Lala'; // The Last page.
  30. Var $ pre_bar = '<'; // The Last Page
  31. Var $ next_bar = '>'; // Next page
  32. Var $ format_left = '[';
  33. Var $ format_right = ']';
  34. Var $ is_ajax = false; // whether the AJAX paging mode is supported
  35. /**
  36. * Private
  37. *
  38. */
  39. Var $ pagebarnum = 10; // control the number of records.
  40. Var $ totalpage = 0; // total number of pages
  41. Var $ ajax_action_name = ''; // AJAX action name
  42. Var $ nowindex = 1; // current page
  43. Var $ url = ""; // url header
  44. Var $ offset = 0;
  45. /**
  46. * 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); // you can specify the current page.
  68. $ This-> _ set_url ($ url); // sets the 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']); // enable the ajax mode
  72. }
  73. /**
  74. * Set the value of the variable name specified in the class. if the change volume does not belong to this class, throw an 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. * Open the 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 displays the 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 for displaying the "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 for displaying the "homepage"
  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 for displaying the "last page"
  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 for displaying 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. * Obtain the limit value in the mysql statement.
  181. *
  182. * @ Return string
  183. */
  184. Function offset ()
  185. {
  186. Return $ this-> offset;
  187. }
  188. /**
  189. * Control the display style by page (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 (). 'Di '. $ this-> select (). 'Page ';
  202. Break;
  203. Case '2 ':
  204. $ This-> next_page = 'next page ';
  205. $ This-> pre_page = 'previous page ';
  206. $ This-> first_page = 'homepage ';
  207. $ This-> last_page = 'Last page ';
  208. Return $ this-> first_page (). $ this-> pre_page (). '[nth '. $ this-> nowindex. 'page] '. $ this-> next_page (). $ this-> last_page (). 'Di '. $ this-> select (). 'Page ';
  209. Break;
  210. Case '3 ':
  211. $ This-> next_page = 'next page ';
  212. $ This-> pre_page = 'previous page ';
  213. $ This-> first_page = 'homepage ';
  214. $ This-> last_page = 'Last page ';
  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 (private method )-----------------------------------------------------------*/
  234. /**
  235. * Set the 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 obtain
  246. If (empty ($ _ SERVER ['query _ string']) {
  247. // When QUERY_STRING does not exist
  248. $ This-> url = $ _ SERVER ['request _ URI ']. "? ". $ This-> page_name." = ";
  249. } Else {
  250. //
  251. If (stristr ($ _ SERVER ['query _ string'], $ this-> page_name. '= ')){
  252. // The address has page parameters.
  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 the current page
  269. *
  270. */
  271. Function _ set_nowindex ($ nowindex)
  272. {
  273. If (empty ($ nowindex )){
  274. // System retrieval
  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. * Return 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 the text displayed by page. for example, by default, _ get_text ('1') will return [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. * Obtain the link address
  308. */
  309. Function _ get_link ($ url, $ text, $ style = ''){
  310. $ Style = (empty ($ style ))? '': 'Class =" '. $ style .'"';
  311. If ($ this-> is_ajax ){
  312. // If the AJAX mode is used
  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 add the database access capability.
  327. Class Page extends _ Page {
  328. Var $ db; // db connected object
  329. Var $ _ SQL _Query = ''; // query the database SQL
  330. Var $ _ Total = 0; // The Total number of queried records.
  331. Var $ _ Rst = array (); // query records.
  332. /**
  333. * The paging query class library.
  334. *
  335. * @ Param String $ the SQL statement used to query SQL records.
  336. * @ Param int $ pagenuber: the number of records per page.
  337. * @ Param int $ pagen current page.
  338. * @ Param String $ parameter. index. php? Xx = B & bb = 33
  339. * @ Param String $ tag of the current page of pname. the default value is index. php? Xx = B & bb = 33 & page = 2 if you have special requirements
  340. You can modify the $ pname parameter. for example, $ pname = 'DB _ page' is changed to index. php? Xx = B & bb = 33 & db_page = 2
  341. * @ Return Mysql_Page
  342. */
  343. Function Page ($ db, $ SQL _query = '', $ max_rows_per_page = 20, $ 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 (array ('total' => $ query_num_rows, 'perpage' => $ max_rows_per_page, 'page _ name' => $ pname, 'URL' => $ url, 'parameters '=> $ parameters ));
  368. }
  369. /**
  370. * Get the record of the current page and return an array.
  371. */
  372. Function findByAll (){
  373. Return $ this-> _ Rst;
  374. }
  375. /**
  376. * Display page information.
  377. *
  378. * @ Param int $ model
  379. */
  380. Function dispaly_links ($ model ){
  381. $ This-> show ($ model );
  382. }
  383. /**
  384. * Number of returned Records.
  385. *
  386. * @ Return Int
  387. */
  388. Function getCount (){
  389. Return $ this-> _ Total;
  390. }
  391. /**
  392. * Number of query result records ..
  393. *
  394. * @ Return Int
  395. */
  396. Function getRows (){
  397. Return count ($ this-> _ Rst );
  398. }
  399. /**
  400. * Execute the query function.
  401. * Calculate the array.
  402. * Private method.
  403. */
  404. Function setData (){
  405. $ This-> _ Rst = $ this-> db-> getResults ($ this-> _ SQL _Query );
  406. }
  407. }
  408. ?>

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.