Home  >  Article  >  Backend Development  >  PHP paging function code sharing

PHP paging function code sharing

WBOY
WBOYOriginal
2016-07-25 08:51:12849browse
For details, see http://qxblog.sinaapp.com/?p=100

  1. //$count is the total number of entries, $page is the current page number, $page_size is the number of entries displayed on each 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. //Judge the current page number
  10. $page=(empty($page)||$page<0)?1:$page;
  11. //Get the current page url
  12. $url = $_SERVER['REQUEST_URI' ];
  13. //Remove the original page parameters in the url to add new page parameters
  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 numbers
  27. $pageoffset = ($page_len-1)/2; //About the number of page numbers Offset
  28. $navs='';
  29. if($pages != 0){
  30. if($page!=1){
  31. $navs.="Homepage "; //First page
  32. $navs.=""; //Previous page
  33. } else {
  34. $navs .= "Homepage";
  35. $navs .= "< ;span class='disabled'>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 current page number right offset exceeds the maximum number of pages
  47. if($page+ $pageoffset>=$pages+1){
  48. $init = $pages-$page_len+1;
  49. }
  50. else
  51. {
  52. //Calculation when both 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. }
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn