Home > Backend Development > PHP Tutorial > A general method to implement data paging in PHP

A general method to implement data paging in PHP

WBOY
Release: 2016-07-25 08:45:27
Original
1019 people have browsed it
  1. function show_page($count,$page,$page_size)
  2. {
  3. $page_count = ceil($count/$page_size); //Calculate the total number of pages
  4. $init=1;
  5. $page_len=7 ;
  6. $max_p=$page_count;
  7. $pages=$page_count;
  8. //Judge the current page number
  9. $page=(empty($page)||$page<0)?1:$page;
  10. //Get the current page url
  11. $url = $_SERVER['REQUEST_URI'];
  12. //Remove the original page parameter in the url to add the new page parameter
  13. $parsedurl=parse_url($url);
  14. $url_query = isset($parsedurl['query ']) ? $parsedurl['query']:'';
  15. if($url_query != ''){
  16. $url_query = preg_replace("/(^|&)page=$page/",'',$ url_query);
  17. $url = str_replace($parsedurl['query'],$url_query,$url);
  18. if($url_query != ''){
  19. $url .= '&';
  20. }
  21. } else {
  22. $url .= '?';
  23. }
  24. //Paging function code
  25. $page_len = ($page_len%2)?$page_len:$page_len+1; //Number of page numbers
  26. $pageoffset = ($page_len-1 )/2; //The left and right offset of the page number
  27. $navs='';
  28. if($pages != 0){
  29. if($page!=1){
  30. $navs.="Homepage "; //First page
  31. $navs.="Previous page"; //Previous page
  32. } else {
  33. $navs .= "Homepage ";
  34. $navs .= "Previous page";
  35. }
  36. if($pages>$page_len)
  37. {
  38. //If the current page is less than or equal to the left Shift
  39. if($page<=$pageoffset){
  40. $init=1;
  41. $max_p = $page_len;
  42. }
  43. else //If the current page is greater than the left offset
  44. {
  45. //If the current page number’s right offset exceeds Maximum number of pages
  46. if($page+$pageoffset>=$pages+1){
  47. $init = $pages-$page_len+1;
  48. }
  49. else
  50. {
  51. //Calculation when both left and right offsets exist
  52. $ init = $page-$pageoffset;
  53. $max_p = $page+$pageoffset;
  54. }
  55. }
  56. }
  57. for($i=$init;$i<=$max_p;$i++)
  58. {
  59. if($i= =$page){$navs.="".$i.'';}
  60. else {$navs.=" ".$i."";}
  61. }
  62. if($page!=$pages)
  63. {
  64. $navs.=" < ;a href="".$url."page=".($page+1)."">Next page ";//Next page
  65. $navs.="Last page"; //Last page
  66. } else {
  67. $navs .= "Next page";
  68. $navs .= "Last page";
  69. }
  70. echo "$navs";
  71. }
  72. }
Copy code

Pagination, PHP


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template