PHP paging function code sharing

Source: Internet
Author: User
PHP paging function code sharing
See http://qxblog.sinaapp.com /? P = 100


  1. // $ Count indicates the total number of entries, $ page indicates the current page number, and $ page_size indicates the number of entries displayed on each page.
  2. Function show_page ($ count, $ page, $ page_size)
  3. {
  4. $ Page_count = ceil ($ count/$ page_size); // calculates the total number of pages.
  5. $ Init = 1;
  6. $ Page_len = 7;
  7. $ Max_p = $ page_count;
  8. $ Pages = $ page_count;
  9. // Determine the current page number
  10. $ Page = (empty ($ page) | $ page <0 )? 1: $ page;
  11. // Obtain the url of the current page
  12. $ Url = $ _ SERVER ['request _ URI '];
  13. // Remove the original page parameter from the url to add a new page parameter.
  14. $ Parsedurl = parse_url ($ url );
  15. $ Url_query = isset ($ parsedurl ['query'])? $ Parsedurl ['query']: '';
  16. If ($ url_query! = ''){
  17. $ Url_query = preg_replace ("/(^ | &) page = $ page/", '', $ url_query );
  18. $ Url = str_replace ($ parsedurl ['query'], $ url_query, $ url );
  19. If ($ url_query! = ''){
  20. $ Url. = '&';
  21. }
  22. } Else {
  23. $ Url. = '? ';
  24. }
  25. // Code of the paging function
  26. $ Page_len = ($ page_len % 2 )? $ Page_len: $ page_len + 1; // Number of page numbers
  27. $ Pageoffset = ($ page_len-1)/2; // page number offset between left and right
  28. $ Navs = '';
  29. If ($ pages! = 0 ){
  30. If ($ page! = 1 ){
  31. $ Navs. = "homepage"; // The first page
  32. $ Navs. = "previous page"; // Previous Page
  33. } Else {
  34. $ Navs. = "homepage ";
  35. $ Navs. = "previous page ";
  36. }
  37. If ($ pages> $ page_len)
  38. {
  39. // If the current page is less than or equal to the left offset
  40. If ($ page <= $ pageoffset ){
  41. $ Init = 1;
  42. $ Max_p = $ page_len;
  43. }
  44. Else // if the current page is greater than the left offset
  45. {
  46. // If the right offset of the current page number exceeds the maximum page number
  47. If ($ page + $ pageoffset >=$ pages + 1 ){
  48. $ Init = $ pages-$ page_len + 1;
  49. }
  50. Else
  51. {
  52. // Calculation when both the left and right offsets exist
  53. $ Init = $ page-$ pageoffset;
  54. $ Max_p = $ page + $ pageoffset;
  55. }
  56. }
  57. }
  58. For ($ I = $ init; $ I <= $ max_p; $ I ++)
  59. {
  60. If ($ I = $ page) {$ navs. = "". $ I .'';}
  61. Else {$ navs. = "". $ I ."";}
  62. }
  63. If ($ page! = $ Pages)
  64. {
  65. $ Navs. = "next page"; // Next page
  66. $ Navs. = "Last page"; // Last Page
  67. } Else {
  68. $ Navs. = "next page ";
  69. $ Navs. = "Last page ";
  70. }
  71. Echo "$ navs ";
  72. }
  73. }

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.