Home  >  Article  >  Backend Development  >  PHP paging code example (adaptive range)

PHP paging code example (adaptive range)

WBOY
WBOYOriginal
2016-07-25 08:52:121126browse
  1. /*

  2. * mysql paging function code
  3. * edit: bbs.it-home.org
  4. *
  5. */
  6. function page($page,$total,$ phpfile,$pagesize=10,$pagelen=7){
  7. $pagecode = '';//Define variables to store the HTML generated by paging
  8. $page = intval($page);//Avoid non-numeric page numbers
  9. $total = intval($total);//Ensure that the total record value type is correct
  10. if(!$total) return array();//If the total number of records is zero, return an empty array
  11. $pages = ceil($total/$pagesize);/ /Calculate total paging
  12. //Process page number legality
  13. if($page<1) $page = 1;
  14. if($page>$pages) $page = $pages;
  15. //Calculate query offset
  16. $offset = $pagesize*($page-1);
  17. //Calculation of page number range
  18. $init = 1;//Starting page number
  19. $max = $pages;//Ending page number
  20. $pagelen = ($pagelen%2 )?$pagelen:$pagelen+1;//Number of page numbers
  21. $pageoffset = ($pagelen-1)/2;//Offset to the left and right of page number

  22. //Generate html

  23. $pagecode='
    ';
  24. $pagecode.="$page/$pages";//Which page, how many pages in total
  25. / /If it is the first page, the connection between the first page and the previous page will not be displayed
  26. if($page!=1){
  27. $pagecode.="<<";//The first page
  28. $pagecode.=" <";//Previous page
  29. }
  30. //When the number of pages is greater than the number of pages, it can be offset
  31. if($pages>$pagelen){
  32. //If the current page is less than or equal to the left offset Shift
  33. if($page<=$pageoffset){
  34. $init=1;
  35. $max = $pagelen;
  36. }else{//If the current page is greater than the left offset
  37. //If the current page number’s right offset exceeds the maximum Number of pages
  38. if($page+$pageoffset>=$pages+1){
  39. $init = $pages-$pagelen+1;
  40. }else{
  41. //Calculation when both left and right offsets exist
  42. $init = $page -$pageoffset;
  43. $max = $page+$pageoffset;
  44. }
  45. }
  46. }
  47. //Generate html
  48. for($i=$init;$i<=$max;$i++){
  49. if($i= =$page){
  50. $pagecode.=''.$i.'';
  51. } else {
  52. $pagecode.="$i";
  53. }
  54. }
  55. if($page!=$pages){
  56. $pagecode.=">";//Next page
  57. $pagecode.=">>";//Last page
  58. }
  59. $pagecode.="
";
  • return array('pagecode'=>$pagecode ,'sqllimit'=>' limit '.$offset.','.$pagesize);
  • }
  • ?>

  • Copy code

    2, added page number jump text frame

    1. $phpfile = 'index.php';//Page file name
    2. $page= isset($_GET['page'])?$_GET['page']:1; //Default page number
    3. $db = mysql_connect('localhost','test','test'); //Link database
    4. mysql_select_db('test',$db); //Select database
    5. $counts = mysql_num_rows(mysql_query( 'select `id` from `test`',$db));//Get the total number of required data
    6. $sql='select `id`,`title` from `test`';//Define query statement SQL
    7. $getpageinfo = page($page,$counts,$phpfile);//Call the function to generate paging HTML and SQL LIMIT clause
    8. $sql.=$getpageinfo['sqllimit'];//Combine the complete SQL statement
    9. $data = $row = array();//Initialize the array
    10. $result = mysql_query($sql,$db);//Get the result set
    11. //Load the data into the $data array
    12. while($row = mysql_fetch_array( $result)){
    13. $data[]=$row;
    14. }
    15. ?>
    16. echo $getpageinfo['pagecode'];//Display pagination html code
    17. ?>
    Copy Code

    3, this paging query css style sheet file used in the code.

    Copy code

    Recommended reading:

    • php and ajax no refresh paging code
    • php article paging implementation code
    • php limit page turning (pagination) code
    • PHP paging class with multiple paging methods
    • PHP pagination code for previous page and next page
    • PHP paging code for the first ten pages and the next ten pages
    • Example of simple php pagination code
    • A good php pagination code
    • A paging function: Previous page Next page
    • A useful php paging class
    • php long article pagination code
    • A practical php paging class
    • Fast php paging class


    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