A good php paging code-php Tutorial

Source: Internet
Author: User
A good php paging code

  1. /**

  2. * Filename: ext_page.class.php
  3. * @ Package: phpbean
  4. * @ Author: feifengxlq
  5. * @ Copyright: Copyright 2006 feifengxlq
  6. * @ License: version 2.0
  7. * Description: Paging type. four paging modes are available.
  8. * 2.0 added: supports custom styles and styles, and supports both 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. Enable AJAX:
  19. $ Ajaxpage = new page (array ('total' => 1000, 'perpage' => 20, 'Ajax '=> 'Ajax _ page ', 'page _ name' => 'test '));
  20. Echo 'MoDe: 1
    '. $ Ajaxpage-> show ();
  21. Use 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 url pages
  30. Var $ next_page = '>'; // Next page
  31. Var $ pre_page = '<'; // Previous Page
  32. Var $ first_page = 'first'; // home page
  33. Var $ last_page = 'Lala'; // The Last page.
  34. Var $ pre_bar = '<'; // The Last Page
  35. Var $ next_bar = '>'; // Next page
  36. Var $ format_left = '[';
  37. Var $ format_right = ']';
  38. Var $ is_ajax = false; // whether the AJAX paging mode is supported

  39. /**

  40. * Private
  41. *
  42. */
  43. Var $ pagebarnum = 10; // control the number of records.
  44. Var $ totalpage = 0; // total number of pages
  45. Var $ ajax_action_name = ''; // AJAX action name
  46. Var $ nowindex = 1; // current page
  47. Var $ url = ""; // url header
  48. Var $ offset = 0;

  49. /**

  50. * 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); // you can specify the current page.
  72. $ This-> _ set_url ($ url); // sets the 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']); // enable the ajax mode
  76. }
  77. /**
  78. * Set the value of the variable name specified in the class. if the change volume does not belong to this class, throw an 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. * Open the 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 displays the 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 for displaying the "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 for displaying the "homepage"
  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 for displaying the "last page"
  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 for displaying 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. * Obtain the limit value in the mysql statement.
  185. *
  186. * @ Return string
  187. */
  188. Function offset ()
  189. {
  190. Return $ this-> offset;
  191. }

  192. /**

  193. * Control the paging 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 (). 'Di '. $ this-> select (). 'Page ';
  206. Break;
  207. Case '2 ':
  208. $ This-> next_page = 'next page ';
  209. $ This-> pre_page = 'previous page ';
  210. $ This-> first_page = 'homepage ';
  211. $ This-> last_page = 'Last page ';
  212. Return $ this-> first_page (). $ this-> pre_page (). '[nth '. $ this-> nowindex. 'page] '. $ this-> next_page (). $ this-> last_page (). 'Di '. $ this-> select (). 'Page ';
  213. Break;
  214. Case '3 ':
  215. $ This-> next_page = 'next page ';
  216. $ This-> pre_page = 'previous page ';
  217. $ This-> first_page = 'homepage ';
  218. $ This-> last_page = 'Last page ';
  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 (private method )---------*/
  232. /** @ Link: http://bbs.it-home.org
  233. * Set the 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 obtain
  244. If (empty ($ _ SERVER ['query _ string']) {
  245. // When QUERY_STRING does not exist
  246. $ This-> url = $ _ SERVER ['request _ URI ']. "? ". $ This-> page_name." = ";
  247. } Else {
  248. //
  249. If (stristr ($ _ SERVER ['query _ string'], $ this-> page_name. '= ')){
  250. // The address has page parameters.
  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 the current page
  267. *
  268. */
  269. Function _ set_nowindex ($ nowindex)
  270. {
  271. If (empty ($ nowindex )){
  272. // System retrieval

  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. * Return 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 the text displayed by page. for example, by default, _ get_text ('1') will return [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. * Obtain the link address
  303. */
  304. Function _ get_link ($ url, $ text, $ style = ''){
  305. $ Style = (empty ($ style ))? '': 'Class =" '. $ style .'"';
  306. If ($ this-> is_ajax ){
  307. // If the AJAX mode is used
  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. ?>

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.