Home >Backend Development >PHP Tutorial >Specific methods of using PHP pagination class_PHP tutorial

Specific methods of using PHP pagination class_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:32:09747browse

In this article we introduce to you in detail the code examples of

PHP paging class:

  1. < ?php
  2. / /In order to avoid errors caused by repeated inclusion of files,
    adds a condition to determine whether the function exists:
  3. if(!function_exists(pageft)){
  4. //Define the function pageft(), the meanings of the three parameters are:
  5. //$totle: the total number of information;
  6. //$displaypg: The number of information displayed on each page, the default setting here is 20;
  7. //$url: Links in paging navigation, in addition to adding different
    The parts other than "page" of the query information are the same as this URL.
  8. //The default value should be set to the URL of this page (i.e. $_SERVER
    ["REQUEST_URI"]), but the
    to the right of the default value can only be a constant , so the default value is set to an empty string, and then set to the URL of this page inside the function.
  9. function pageft($totle,$displaypg=20,$url='' ){
  10. //Define several global variables:
  11. //$page: current page number;
  12. //$firstcount: the starting item of (database) query;
  13. //$pagenav: page navigation bar code, it is not output inside the function;
  14. //$_SERVER: necessary to read the URL "$_SERVER["REQUEST_URI"]" of this page.
  15. global $page,$firstcount,$pagenav,$_SERVER;
  16. //To make the "$displaypg" here accessible from outside the function ”
    and set it as a global variable as well. Note that after a variable is redefined
    as a global variable, the original value is overwritten, so it is reassigned here.
  17. $GLOBALS["displaypg"]=$displaypg;
  18. if(!$page) $page=1;
  19. //If $url is used By default, which is an empty value, the value is assigned to the URL of this page:
  20. if(!$url){ $url=$_SERVER["REQUEST_URI"];}
  21. //URL analysis:
  22. $parse_urlparse_url=parse_url($url);
  23. $ url_query=$parse_url["query"]; //Get the query string of the URL separately
  24. if($url_query){
  25. //Because the URL may contain page number information,
    We're going to remove it so we can add the new page number information.
  26. //Regular expressions are used here
  27. $url_query=ereg_replace("(^|&)page=$ page","",$url_query);
  28. //Replace the query string of the processed URL with the query string of the original URL:
  29. $url=str_replace($parse_url["query "],$url_query,$url);
  30. //Add page query information after the URL, but wait for assignment:
  31. if( $url_query) $url.="&page"; else $url.="page";
  32. }else {
  33. $url.= "?page";
  34. }
  35. //Page number calculation:
  36. $lastpg=ceil($totle/ $displaypg); //The last page is also the total number of pages
  37. $page=min($lastpg,$page);
  38. $prepg=$page-1; //Previous page
  39. $nextpg=( $page==$lastpg ? 0 : $page+1); //Next page
  40. $firstcount=($page-1)*$displaypg;
  41. //Start paging navigation bar code :
  42. $pagenav="Showing page ".($totle?($firstcount+1):0)."
    B>-<B> ;".min($firstcount+$displaypg,$totle)."
    B> records, total $totle records< BR>";
  43. //If there is only one page, jump out of the function:
  44. if($lastpg<=1) return false;
  45. $pagenav.=" < a href='$url=1'> Home page< /a> "; 🎜>=
  46. " < a href='$url=$prepg'>Previous page< /a> "; else $pagenav.=" Previous page ";
  47. if($nextpg) $pagenav .="
    < a href='$url=$nextpg'>Next page< /a> "
    ; else $pagenav.=" Next page "
  48. ;
  49. //Drop-down jump list, loop list All page numbers: $pagenav.=
  50. " to page < ; select name='topage' size='1' onchange='window.location="
  51. $url="+this.value'>
    n"; for($i=1
  52. ;$i
  53. <=$lastpg;$i++){ if($ i==$page) $pagenav.
  54. =
  55. " < option value='$i' selected>$i< /option>n"; 🎜>pagenav.=" $i< /option>n"
    ; 🎜>
    =
    "< /select> pages of $lastpg pages"
  56. ; } }
    ?>
  57. The above is the detailed usage of PHP paging class. I hope it will be helpful to everyone. http://www.bkjia.com/PHPjc/446164.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446164.html
  58. TechArticleIn this article, we introduce code examples of PHP pagination classes in detail: ?php //For To avoid errors caused by repeated inclusion of files, a condition is added to determine whether the function exists:...
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