PHP Paging function code sharing

Source: Internet
Author: User
See http://qxblog.sinaapp.com/?p=100

  1. $count is the total number of entries, $page the current page number, $page _size the number of entries per page
  2. function Show_page ($count, $page, $page _size)
  3. {
  4. $page _count = ceil ($count/$page _size); Calculate the total number of pages
  5. $init = 1;
  6. $page _len=7;
  7. $max _p= $page _count;
  8. $pages = $page _count;
  9. Judging the current page number
  10. $page = (Empty ($page) | | $page <0)? 1: $page;
  11. Gets the current page URL
  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. Paging function code
  26. $page _len = ($page _len%2)? $page _len: $page _len+1; Number of page number
  27. $pageoffset = ($page _len-1)/2; Left and right offset of page number
  28. $navs = ";
  29. if ($pages! = 0) {
  30. if ($page!=1) {
  31. $navs. = "Home";//First page
  32. $navs. = "Previous page";//Previous Page
  33. } else {
  34. $navs. = " home ";
  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 Max page count
  47. if ($page + $pageoffset >= $pages + 1) {
  48. $init = $pages-$page _len+1;
  49. }
  50. Else
  51. {
  52. Calculations when left and right offsets are present
  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";//Last Page
  67. } else {
  68. $navs. = " next page ";
  69. $navs. = " last ";
  70. }
  71. echo "$navs";
  72. }
  73. }
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.