Home >Backend Development >PHP Tutorial >A PHP paging function

A PHP paging function

WBOY
WBOYOriginal
2016-07-25 08:44:27931browse

PHP paging code call: $start=show_page($query,$page,$link,$offset); where: $start is the starting record, $query is the full record retrieval SQL statement, $page is the current page number, $link is the passed The page parameter $offset is the number of records displayed on each page

  1. //======function.php======
  2. //======Paging function=== =======
  3. function show_page($query,$page,$link,$offset)
  4. {
  5. $db = new mysql();
  6. $result = $db->query($query);
  7. $Page_size = $offset; //Get the maximum number of orders displayed on each page
  8. $count = $db->affected_rows($result); //Total number of orders
  9. $page_count = ceil($count/$Page_size); / /Calculate the total number of pages
  10. $init=1;
  11. $page_len=7;
  12. $max_p=$page_count;
  13. $pages=$page_count;
  14. //Judge the current page number
  15. $page=(empty($page )||$page<0)?1:$page;
  16. $start=$Page_size*($page-1);
  17. //Paging function code
  18. $page_len = ($page_len%2)?$page_len:$ pagelen+1; //The number of page numbers
  19. $pageoffset = ($page_len-1)/2; //The left and right offset of the number of page numbers
  20. $key="$count in total";
  21. $key.="$ page/$pages "; //What page, how many pages in total
  22. if($page!=1){
  23. $key.="First page "; //First page
  24. $key.="Previous page"; //Previous page
  25. }
  26. else
  27. {
  28. $key.="First page";//First Page
  29. $key.="Previous page"; //Previous page
  30. }
  31. if($pages>$page_len)
  32. {
  33. //If the current page is less than or equal to the left offset
  34. if($page<=$pageoffset ){
  35. $init=1;
  36. $max_p = $page_len;
  37. }
  38. else //If the current page is greater than the left offset
  39. {
  40. //If the right offset of the current page number exceeds the maximum number of pages
  41. if($page+$ pageoffset>=$pages+1){
  42. $init = $pages-$page_len+1;
  43. }
  44. else
  45. {
  46. //Calculation when both left and right offsets exist
  47. $init = $page-$pageoffset;
  48. $ max_p = $page+$pageoffset;
  49. }
  50. }
  51. }
  52. for($i=$init;$i<=$max_p;$i++)
  53. {
  54. if($i==$page){$key.=' ['.$i.']';}
  55. else {$key.=" ";}
  56. }
  57. if($page!=$pages)
  58. {
  59. $key.=" Next page ";//Next page
  60. $key.="Last page"; //Last page
  61. }
  62. else
  63. {
  64. $key.= "Next page"; //Next page
  65. $key.="Last page"; //Last page
  66. }
  67. echo "$key

    ";
  68. return $start;
  69. }
  70. ?>
Copy code

Call example

[code]
Pagination, PHP


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