Php paging code instance (adaptive range)

Source: Internet
Author: User
Php paging code instance (adaptive range)

  1. /*

  2. * Mysql paging function code
  3. * Edit: bbs.it-home.org
  4. *
  5. */
  6. Function page ($ page, $ total, $ phpfile, $ pagesize = 10, $ pagelen = 7 ){
  7. $ Pagecode = ''; // defines variables and stores HTML generated by page.
  8. $ Page = intval ($ page); // avoid non-numeric page numbers
  9. $ Total = intval ($ total); // ensure that the total record value type is correct
  10. If (! $ Total) return array (); // The total number of records is zero. an empty array is returned.
  11. $ Pages = ceil ($ total/$ pagesize); // calculates the total page size.
  12. // Process page validity
  13. If ($ page <1) $ page = 1;
  14. If ($ page> $ pages) $ page = $ pages;
  15. // Calculate the query offset
  16. $ Offset = $ pagesize * ($ page-1 );
  17. // Page number range calculation
  18. $ Init = 1; // number of start page codes
  19. $ Max = $ pages; // Number of ending pages
  20. $ Pagelen = ($ pagelen % 2 )? $ Pagelen: $ pagelen + 1; // Number of page numbers
  21. $ Pageoffset = ($ pagelen-1)/2; // page number offset between left and right

  22. // Generate html

  23. $ Pagecode ='

    ';

  24. $ Pagecode. = "$ page/$ pages"; // page
  25. // If the first page is displayed, the connection between the first page and the previous page is not displayed.
  26. If ($ page! = 1 ){
  27. $ Pagecode. = "<"; // page 1
  28. $ Pagecode. = "<"; // Previous Page
  29. }
  30. // Offset when the number of pages is greater than the number of pages
  31. If ($ pages> $ pagelen ){
  32. // If the current page is less than or equal to the left offset
  33. If ($ page <= $ pageoffset ){
  34. $ Init = 1;
  35. $ Max = $ pagelen;
  36. } Else {// if the current page is greater than the left offset
  37. // If the right offset of the current page number exceeds the maximum page number
  38. If ($ page + $ pageoffset >=$ pages + 1 ){
  39. $ Init = $ pages-$ pagelen + 1;
  40. } Else {
  41. // Calculation when both the left and right offsets exist
  42. $ Init = $ page-$ pageoffset;
  43. $ Max = $ page + $ pageoffset;
  44. }
  45. }
  46. }
  47. // Generate html
  48. For ($ I = $ init; $ I <= $ max; $ I ++ ){
  49. If ($ I = $ page ){
  50. $ Pagecode. = ''. $ I .'';
  51. } Else {
  52. $ Pagecode. = "$ I ";
  53. }
  54. }
  55. If ($ page! = $ Pages ){
  56. $ Pagecode. = ">"; // Next page
  57. $ Pagecode. = ">"; // Last Page
  58. }
  59. $ Pagecode. ="

    ";
  60. Return array ('pagecode' => $ pagecode, 'sqllimit' => 'limit'. $ offset. ','. $ pagesize );
  61. }
  62. ?>

2. added a page jump text box.

  1. $ Phpfile = 'index. php'; // page file name
  2. $ Page = isset ($ _ GET ['Page'])? $ _ GET ['Page']: 1; // Default page number
  3. $ Db = mysql_connect ('localhost', 'test', 'test'); // link to the database
  4. Mysql_select_db ('test', $ db); // select a database
  5. $ Counts = mysql_num_rows (mysql_query ('select' ID' from 'test', $ db); // obtain the total number of required data records
  6. $ SQL = 'SELECT 'id', 'title' from 'test'; // defines the query statement SQL
  7. $ Getpageinfo = page ($ page, $ counts, $ phpfile); // call a function to generate paging HTML and SQL LIMIT clauses
  8. $ SQL. = $ getpageinfo ['sqllimit']; // combine the complete SQL statement
  9. $ Data = $ row = array (); // Initialize an array
  10. $ Result = mysql_query ($ SQL, $ db); // obtain the result set
  11. // Load the data into the $ data array
  12. While ($ row = mysql_fetch_array ($ result )){
  13. $ Data [] = $ row;
  14. }
  15. ?>
  16. Echo $ getpageinfo ['pagecode']; // display the html code of the page
  17. ?>

3. the css style table file used in the paging query code.

Recommended reading:
  • Php and ajax without refreshing paging code
  • Php article paging implementation code
  • Php limit paging code
  • Php paging class with multiple paging methods
  • Php paging code for the previous and next pages
  • Top 10 pages and top 10 pages of php paging code
  • Example of simple php paging code
  • A good php paging code
  • One paging function: next page on the previous page
  • A handy php paging class
  • Php long article paging code
  • A Practical php paging class
  • Quick php paging

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.