Phpmysql paging class (getting started with php)

Source: Internet
Author: User
Phpmysql paging class (getting started with php)

  1. /***

  2. * Php mysql paging class
  3. * Organize 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; // minimum number of page entries
  12. Private $ _ endPage; // The end code of the page
  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 = 10, $ style = 1 ){
  33. If ($ style = 1 ){
  34. If ($ barLn % 2! = 0 ){
  35. $ Midder = ceil ($ barLn/2 );
  36. $ Big_repair = $ midder-1; // when an integer is obtained by the preceding method, 1 is subtracted here, and 1 is incremented.
  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); // at least from the first page
  55. $ This-> _ endPage = min ($ this-> _ endPage, $ this-> _ pageMax); // only the last page can be reached.
  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. ='

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.