PHP functions implement paging effect, php text paging and numeric paging

Source: Internet
Author: User
PHP functions implement paging effect, php text paging and numeric paging

  1. // Pagination
  2. /**
  3. * $ PageType paging Type 1: numeric paging 2: text paging
  4. * You can pass $ pageTotal, $ page, $ total, and other data as parameters, or use paging as a global variable (recommended)
  5. */
  6. Function paging ($ pageType)
  7. {
  8. Global $ pageTotal, $ page, $ total;
  9. If ($ pageType = 1)
  10. {
  11. Echo'

    ';

  12. Echo'
      ';
    • For ($ I = 0; $ I <$ pageTotal; $ I ++)
    • {
    • If ($ page = ($ I + 1 ))
    • {
    • Echo'
    • '. ($ I + 1 ).'
    • ';
    • }
    • Else
    • {
    • Echo'
    • '. ($ I + 1 ).'
    • ';
    • }
    • }
    • Echo'
    ';
  13. Echo'

    ';
  14. }
  15. Else if ($ pageType = 2)
  16. {
  17. Echo'

    ';

  18. Echo'
      ';
    • Echo'
    • '. $ Page.'/'. $ pageTotal.' page |
    • ';
    • Echo'
    • Total'. $ Total .'Members |
    • ';
    • // Page 1
    • If ($ page = 1)
    • {
    • Echo'
    • Homepage |
    • ';
    • Echo'
    • Previous Page |
    • ';
    • }
    • Else
    • {
    • // $ _ SERVER ["SCRIPT_NAME"] get the name of the current script for ease of transplantation
    • // You can also customize constants. the constant value is consistent with the script file name.
    • Echo'
    • Homepage |
    • ';
    • Echo'
    • Previous Page |
    • ';
    • }
    • // Last Page
    • If ($ page = $ pageTotal)
    • {
    • Echo'
    • Next Page |
    • ';
    • Echo'
    • Last page |
    • ';
    • }
    • Else
    • {
    • Echo'
    • Next Page |
    • ';
    • Echo'
    • Last page |
    • ';
    • }
    • Echo'
    ';
  19. Echo'

    ';
  20. }
  21. }

Parameter explanation: $ pageTotal indicates the total number of pages, $ page indicates the current page, and $ total indicates the total number of data retrieved from the database;

2. encapsulate all parameters

  1. // Split paging parameters
  2. /**
  3. * $ SQL: an SQL statement that obtains the total number of data.
  4. * $ Size number of entries displayed on each page
  5. */
  6. Function pageParam ($ SQL, $ size)
  7. {
  8. // Set global variables for all involved parameters
  9. // $ Where does pagestart start from?
  10. // $ Total number of records $ page a page $ total number of pageTotal pages
  11. Global $ pagestart, $ pagesize, $ total, $ page, $ pageTotal;
  12. $ Pagesize = $ size;
  13. // Obtain the total number of data items
  14. $ Total = mysql_num_rows (queryDB ($ SQL ));
  15. // Handle the error. First, determine whether the error exists.
  16. If (isset ($ _ GET ['Page'])
  17. {
  18. // Specific page
  19. $ Page = $ _ GET ['Page'];
  20. // Determine whether it is empty (0 is empty)/less than 0/whether it is a number
  21. If (empty ($ page) | $ page <0 |! Is_numeric ($ page ))
  22. {
  23. $ Page = 1;
  24. }
  25. Else
  26. {
  27. $ Page = intval ($ page); // integer to prevent decimal occurrence
  28. }
  29. }
  30. Else
  31. {
  32. // Page 1st is displayed during initialization.
  33. $ Page = 1;
  34. }
  35. // Reset the database
  36. If ($ total = 0)
  37. {
  38. // Set to 1
  39. $ PageTotal = 1;
  40. }
  41. Else
  42. {
  43. // The total number of pages (one-to-one integer)
  44. $ PageTotal = ceil ($ total/$ pagesize );
  45. }
  46. // The page number is greater than the total page number $ total
  47. If ($ page> $ pageTotal)
  48. {
  49. $ Page = $ pageTotal;
  50. }
  51. // When the page starts from a record
  52. $ Pagestart = ($ page-1) * $ pagesize;
  53. }

Parameter explanation: $ pagestart indicates the number of records displayed on each page starting from a certain record. $ pagesize indicates the number of records displayed on each page.

3. in use, call pageParam before calling paging.

  1. /**
  2. * The first SQL statement that can obtain the total number of data
  3. * Number of Entries displayed on the second page
  4. */
  5. PageParam ("select userid from user", 2 );
  6. // Page Type 1: digital page 2: text page
  7. Paging (2 );
  8. ?>

4. select the call location based on the actual situation. text paging:

  1. // Page Type 1: digital page 2: text page
  2. Paging (1 );
  3. ?>

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.