Php paging call instance tutorial-php Tutorial

Source: Internet
Author: User
Php paging class call instance tutorial

  1. /**

  2. * Filename: ext_page.class.php
  3. * @ Package: phpbean
  4. * Description: exclusive paging mode. four paging modes are used by default, which are similar to baidu and google paging modes.
  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. 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. Use 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 url pages. For example, xxx. php? PB_page = PB_page in 2
  25. Var $ next_page = '>'; // Next page
  26. Var $ pre_page = '<'; // Previous Page
  27. Var $ first_page = 'first'; // home page
  28. Var $ last_page = 'Lala'; // The Last page.
  29. Var $ pre_bar = '<'; // The Last Page
  30. Var $ next_bar = '>'; // Next page
  31. Var $ format_left = '';
  32. Var $ format_right = '';
  33. Var $ is_ajax = false; // whether the AJAX paging mode is supported

  34. /**

  35. * Private
  36. *
  37. */
  38. Var $ pagebarnum = 10; // control the number of records.
  39. Var $ totalpage = 0; // total number of pages
  40. Var $ ajax_action_name = ''; // AJAX action name
  41. Var $ nowindex = 1; // current page
  42. Var $ url = ""; // url header
  43. Var $ offset = 0;

  44. /**

  45. * 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); // you can specify the current page.
  67. $ This-> _ set_url ($ url); // sets the 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']); // enable the ajax mode
  71. }
  72. /**
  73. * Set the value of the variable name specified in the class. if the change volume does not belong to this class, throw an 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. * Open the 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 displays the 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 for displaying the "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 for displaying the "homepage"
  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 for displaying the "last page"
  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 for displaying 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. * Obtain the limit value in the mysql statement.
  180. *
  181. * @ Return string
  182. */
  183. Function offset ()
  184. {
  185. Return $ this-> offset;
  186. }

  187. /**

  188. * Control the display style by page (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 (). 'Di '. $ this-> select (). 'Page ';
  201. Break;
  202. Case '2 ':
  203. $ This-> next_page = 'next page ';
  204. $ This-> pre_page = 'previous page ';
  205. $ This-> first_page = 'homepage ';
  206. $ This-> last_page = 'Last page ';
  207. Return $ this-> first_page (). $ this-> pre_page (). '[nth '. $ this-> nowindex. 'page] '. $ this-> next_page (). $ this-> last_page (). 'Di '. $ this-> select (). 'Page ';
  208. Break;
  209. Case '3 ':
  210. $ This-> next_page = 'next page ';
  211. $ This-> pre_page = 'previous page ';
  212. $ This-> first_page = 'homepage ';
  213. $ This-> last_page = 'Last page ';
  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 (private method )-----------------------------------------------------------*/
  229. /**
  230. * Set the 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 obtain
  241. If (empty ($ _ SERVER ['query _ string']) {
  242. // When QUERY_STRING does not exist
  243. $ This-> url = $ _ SERVER ['request _ URI ']. "? ". $ This-> page_name." = ";
  244. } Else {
  245. //
  246. If (stristr ($ _ SERVER ['query _ string'], $ this-> page_name. '= ')){
  247. // The address has page parameters.
  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 the current page
  264. *
  265. */
  266. Function _ set_nowindex ($ nowindex)
  267. {
  268. If (empty ($ nowindex )){
  269. // System retrieval

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

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.