How to make PHP paging display

Source: Internet
Author: User
Tags pear
  1. Establishing a database connection

  2. $link = mysql_connect ("localhost", "Mysql_user", "Mysql_password")
  3. Or Die ("Could Not connect:". Mysql_error ());

  4. Get the current number of pages

  5. if (Isset ($_get[' page ')) {
  6. $page = intval ($_get[' page ');
  7. }
  8. else{
  9. $page = 1;
  10. }

  11. Quantity per page

  12. $PageSize = 10;

  13. Get Total data volume

  14. $sql = "SELECT count (*) as amount from table";
  15. $result = mysql_query ($sql);
  16. $row = Mysql_fetch_row ($result);
  17. $amount = $row [' Amount '];

  18. Count how many pages there are in total

  19. if ($amount) {
  20. if ($amount < $page _size) {$page _count = 1;}//If the total data volume is less than $pagesize, then there is only one page
  21. if ($amount% $page _size) {//Take the total amount of data divided by the remainder of each page
  22. $page _count = (int) ($amount/$page _size) + 1; If there is an excess, the number of pages equals the total amount of data divided by the result of each page plus one
  23. }else{
  24. $page _count = $amount/$page _size; If there is no remainder, the number of pages equals the total data amount divided by the result of each page
  25. }
  26. }
  27. else{
  28. $page _count = 0;
  29. }

  30. Page link

  31. $page _string = ";
  32. if ($page = = 1) {
  33. $page _string. = ' first page | prev | ';
  34. }
  35. else{
  36. $page _string. = ' first page | prev | ';
  37. }

  38. if ($page = = $page _count) | | ($page _count = = 0)) {

  39. $page _string. = ' next page | last ';
  40. }
  41. else{
  42. $page _string. = ' next page | last ';
  43. }

  44. Get data to return results in two-dimensional array format

  45. if ($amount) {
  46. $sql = "SELECT * from table ORDER BY id desc limit". ($page-1) * $page _size. ", $page _size";
  47. $result = mysql_query ($sql);
  48. while ($row = Mysql_fetch_row ($result)) {
  49. $rowset [] = $row;
  50. }
  51. }else{
  52. $rowset = Array ();
  53. }
  54. There is no code to display the results, that is not the scope of the discussion, as long as a foreach can be used to easily use the resulting two-dimensional array to display the results
  55. ?>

Copy Code

4. oo Style code the database connection in the following code is handled using the Pear DB Class

  1. FileName:Pager.class.php

  2. Paging class, which is used only to process data structures and is not responsible for working with the display
  3. Class Pager
  4. {
  5. var $PageSize; Number of pages per page
  6. var $CurrentPageID; Current number of pages
  7. var $NextPageID; Next page
  8. var $PreviousPageID; Previous page
  9. var $numPages; Total pages
  10. var $numItems; Total Record Count
  11. var $isFirstPage; Whether the first page
  12. var $isLastPage; Whether the last page
  13. var $sql; SQL query Statements

  14. function Pager ($option)

  15. {
  16. Global $db;
  17. $this->_setoptions ($option);

  18. Total number of bars

  19. if (!isset ($this->numitems))
  20. {
  21. $res = $db->query ($this->sql);
  22. $this->numitems = $res->numrows ();
  23. }

  24. Total pages

  25. if ($this->numitems > 0)
  26. {
  27. if ($this->numitems < $this->pagesize) {$this->numpages = 1;}
  28. if ($this->numitems% $this->pagesize)
  29. {
  30. $this->numpages= (int) ($this->numitems/$this->pagesize) + 1;
  31. }
  32. Else
  33. {
  34. $this->numpages = $this->numitems/$this->pagesize;
  35. }
  36. }
  37. Else
  38. {
  39. $this->numpages = 0;
  40. }

  41. Switch ($this->currentpageid)

  42. {
  43. Case $this->numpages = = 1:
  44. $this->isfirstpage = true;
  45. $this->islastpage = true;
  46. Break

  47. Case 1:

  48. $this->isfirstpage = true;
  49. $this->islastpage = false;
  50. Break

  51. Case $this->numpages:

  52. $this->isfirstpage = false;
  53. $this->islastpage = true;
  54. Break

  55. Default

  56. $this->isfirstpage = false;
  57. $this->islastpage = false;
  58. }

  59. if ($this->numpages > 1)

  60. {
  61. if (! $this->islastpage) {$this->nextpageid = $this->currentpageid + 1;}
  62. if (! $this->isfirstpage) {$this->previouspageid = $this->currentpageid-1;}
  63. }
  64. return true;
  65. }

  66. /***

  67. *
  68. * Returns the database connection for the result set
  69. * When the result set is relatively large, you can use this method to get the database connection directly, and then traverse outside the class, which is less expensive
  70. * If the result set is not very large, you can get the result of the two-dimensional array format directly using the Getpagedata method
  71. * The Getpagedata method also calls this method to get the result.
  72. *
  73. ***/
  74. function Getdatalink ()
  75. {
  76. if ($this->numitems)
  77. {
  78. Global $db;
  79. $PageID = $this->currentpageid;
  80. $from = ($PageID-1) * $this->pagesize;
  81. $count = $this->pagesize;
  82. $link = $db->limitquery ($this->sql, $from, $count); Ensuring database compatibility with the Pear Db::limitquery method
  83. return $link;
  84. }
  85. Else
  86. {
  87. return false;
  88. }
  89. }

  90. /***

  91. *
  92. * Returns the result set in a two-dimensional array format
  93. *
  94. ***/
  95. function Getpagedata ()
  96. {
  97. if ($this->numitems)
  98. {
  99. if ($res = $this->getdatalink ())
  100. {
  101. if ($res->numrows ())
  102. {
  103. while ($row = $res->fetchrow ())
  104. {
  105. $result [] = $row;
  106. }
  107. }
  108. Else
  109. {
  110. $result = Array ();
  111. }
  112. return $result;
  113. }
  114. Else
  115. {
  116. return false;
  117. }
  118. }
  119. Else
  120. {
  121. return false;
  122. }
  123. }

  124. function _setoptions ($option)

  125. {
  126. $allow _options = Array (
  127. ' PageSize ',
  128. ' Currentpageid ',
  129. ' SQL ',
  130. ' NumItems '
  131. );

  132. foreach ($option as $key = $value)

  133. {
  134. if (In_array ($key, $allow _options) && ($value! = null))
  135. {
  136. $this $key = $value;
  137. }
  138. }
  139. return true;
  140. }
  141. }
  142. ?>

Copy Code

#------------------------

  1. FileName:test_pager.php

  2. This is a simple example code that omits the code that uses the Pear DB class to establish a database connection
  3. Require "Pager.class.php";

  4. if (Isset ($_get[' page '))

  5. {
  6. $page = (int) $_get[' page ';
  7. }
  8. Else
  9. {
  10. $page = 1;
  11. }

  12. $sql = "SELECT * from table order by id";

  13. $pager _option = Array (
  14. "SQL" = $sql,
  15. "PageSize" = 10,
  16. "Currentpageid" = $page
  17. );

  18. if (Isset ($_get[' NumItems '))

  19. {
  20. $pager _option[' numitems '] = (int) $_get[' NumItems '];
  21. }

  22. $pager = @new Pager ($pager _option);

  23. $data = $pager->getpagedata ();

  24. if ($pager->isfirstpage)

  25. {
  26. $turnover = "Home Page | prev |";
  27. }
  28. Else
  29. {
  30. $turnover = "NumItems." > Home | Previouspageid. " &numitems= ". $pager->numitems." > Prev | ";
  31. }

  32. if ($pager->islastpage)

  33. {
  34. $turnover. = "Next page | last";
  35. }
  36. Else
  37. {
  38. $turnover. = "Nextpageid." &numitems= ". $pager->numitems." > next page |numpages. " &numitems= ". $pager->numitems." > Last ";
  39. }
  40. ?>

Copy Code

Description: This class only handles the data and is not responsible for the display, because I think it is somewhat reluctant to put the data processing and the display of the results into a class. When the situation and requirements are changeable, it is better to treat them according to the results given by the class, and the best way to do this is to inherit a subclass from the pager class to display different pagination, such as displaying a user page list:

    1. !--? php class Memberpager extends Pager

    2. {
    3. function sh Owmemberlist ()
    4. {
    5. global $db;
    6. $data = $this->getpagedata ();
    7. //code to display the result
    8. //...
    9. }
    10. }

    11. //Call

    12. if (isset ($_get[' page '))
    13. {
    14. $page = (int) $_get[' page '];
    15. }
    16. else
    17. {
    18. $page = 1;
    19. }

    20. $sql = "SELECT * from the members order by ID";

    21. $pager _option = Array (
    22. "sql" = = $sql,
    23. "PageSize" = ten,
    24. "Currentpageid" = $pag e
    25. );

    26. If (isset ($_get[' NumItems '))

    27. {
    28. $pager _option[' numitems '] = (int) $_get[' Nu Mitems '];
    29. }
    30. $pager = @new Memberpager ($pager _option);
    31. $pager->showmemberlist ();
    32. ?

Copy Code

The second area to be explained is the compatibility of different databases, and it is not the same to intercept a result in different databases. Mysql:select * FROM table limit offset, rowspgsql:select * from table limit m offset n ...

Therefore, the Limitquery method of the Pear DB Class is required to obtain the result in the class.

  • 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.