PHP MySQL paging class (beginner PHP)

Source: Internet
Author: User
  1. /***

  2. * PHP MySQL Paging class
  3. * Finishing http://bbs.it-home.org
  4. */
  5. Class pagination{
  6. Private $_result;
  7. Private $_count; Number of records
  8. Private $_pagemax; Maximum page
  9. Private $_page; Current page
  10. Private $_url;
  11. Private $_startpage;//pagination at least
  12. Private $_endpage; Page Bar Stop code
  13. Private $_nextpage; Previous page
  14. Private $_prepage; Next page

  15. function __construct ($table, $pageSize, $getPage) {

  16. $this->_result = $GLOBALS [' DB ']->query (' SELECT * from ' $table);
  17. $this->_count = $GLOBALS [' db ']->getrecordnum ();
  18. $this->_pagemax = ceil ($this->_count/$pageSize);
  19. $this->_result = ";

  20. if ($_get[$getPage] = = ")

  21. $this->_page = 1;
  22. Else
  23. $this->_page= Max (Intval ($_get[$getPage]), 1);
  24. $this->_page = min ($this->_page, $this->_pagemax);

  25. $offset = ($this->_page-1) * $pageSize;

  26. $sql = ' SELECT * from '. $table. ' LIMIT '. $offset. ', '. $pageSize;
  27. $this->_result = $GLOBALS [' db ']->query ($sql);
  28. }
  29. function Getrecord () {
  30. return $GLOBALS [' DB ']->getrecord ();
  31. }
  32. function Getpagebar ($url = '? ', $barLn = ten, $style = 1) {
  33. if ($style = = 1) {
  34. if ($barLn% 2 = 0) {
  35. $midder = Ceil ($barLn/2);
  36. $big _repair = $midder-1;//When the above is rounded up by a method, this is minus 1, and the reverse is plus 1.
  37. }else{
  38. $midder = $big _repair = $barLn/2;
  39. }
  40. $SML _repair = $midder-1;
  41. $this->_startpage = ($this->_page + $midder) > $this->_pagemax? $this->_pagemax-$barLn: $this->_page-$sml _repair;
  42. $this->_endpage = $this->_page < $midder? $barLn: $this->_page + $big _repair;

  43. }elseif ($style = = 2) {
  44. if ($this->_page% $barLn = = 0) {
  45. $this->_startpage = $this->_page;
  46. }else{
  47. $this->_startpage = ($this->_page > $barLn)? $this->_page-($this->_page% $barLn): 1;
  48. }
  49. $this->_endpage = $this->_startpage + $barLn-1;
  50. }
  51. $this->_url = $url;
  52. $this->_nextpage = $this->_page + 1;
  53. $this->_prepage = $this->_page-1;
  54. $this->_startpage = max ($this->_startpage, 1);//start at least from the first page
  55. $this->_endpage = min ($this->_endpage, $this->_pagemax);//Up to Last
  56. $this->_result = ' current is: '. $this->_page. ' /'. $this->_pagemax ' page, Total '. $this->_count. ' Records ';
  57. if ($this->_page > 1)
  58. $this->_result. = ' _url. ' page=1 ' >
  59. 9_url. ' page= '. $this->_prepage. ' " >
  60. 3
  61. ';
  62. Else
  63. $this->_result. = ' 9
  64. 3 ';
  65. for ($i = $this->_startpage; $i <= $this->_endpage; $i + +) {
  66. if ($this->_page = = $i)
  67. $this->_result. = '. $i. ';
  68. Else
  69. $this->_result.= ' _url. ' page= '. $i. ' > '. $i. ';
  70. }
  71. if ($this->_page! = $this->_pagemax) {
  72. $this->_result. = ' _url. ' Page= '. $this->_nextpage. ' " >4 ';
  73. $this->_result. = ' _url. ' page= '. $this->_pagemax. ' " : ';
  74. } else {
  75. $this->_result.= ' 4: ';
  76. }
  77. $this->_result.= '
  78. ';
  79. return $this->_result;
  80. }
  81. }
  82. ?>

Copy Code

2. Invoking the example

    1. $page = new Pagination ($table, 5, ' page ');
    2. while ($row = $page->getrecord ()) {
    3. echo $row [0], '
      ';
    4. }
    5. echo $page->getpagebar ('? ', 8, 1);
    6. ?>
Copy Code
  • 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.