Home > Backend Development > PHP Tutorial > Principle analysis of php+mysql paging query code

Principle analysis of php+mysql paging query code

WBOY
Release: 2016-07-25 08:52:15
Original
1070 people have browsed it
  •  
  •    
  •   
  •   < ?php
  •   do {
  •    $i++;
  •    ?>
  •   
  •   
  •   
  •     }
  •  while ($myrow = mysql_fetch_array ($rs));
  •   echo "
    1. $pagesize=10; //Set the number of records displayed on each page
    2. $conn=mysql_connect("localhost","root",""); //Connect to the database
    3. $rs=mysql_query("select count(*) from tb_product",$conn); //Get the total number of records $rs
    4. $myrow = mysql_fetch_array($rs);
    5. $numrows=$myrow[0];
    6. //Calculate the total number of pages
    7. $pages= intval($numrows/$pagesize);
    8. //Determine the page number setting
    9. if (isset($_GET['page'])){
    10.  $page=intval($_GET['page']);
    11. }
    12. else {
    13.  $page=1; // Otherwise, set to the first page
    14. }
    Copy code

    3. Create the use case table myTable

    1. php pagination - bbs.it-home.org
    2.  $conn=mysql_connect("localhost","root","");
    3.  //Set each The number of records displayed on one page
    4.  $pagesize=1;
    5.  mysql_select_db("mydata",$conn);
    6.  //Get the total number of records $rs, and calculate the total number of pages using
    7.  $rs=mysql_query("select count(*) from tb_product",$conn);
    8.  $myrow = mysql_fetch_array($rs);
    9.  $numrows=$myrow[0];
    10.  //Calculate the total number of pages
    11.  $pages=intval($numrows/$pagesize);
    12.  if ( $numrows%$pagesize)
    13.  $pages++;
    14.  //Set the number of pages
    15.  if (isset($_GET['page'])){
    16.  $page=intval($_GET['page']);
    17.  }
    18.  else {
    19.   //Set as the first page
    20.   $page=1;
    21.  }
    22.  //Calculate the record offset
    23.  $offset=$pagesize*($page - 1);
    24.  //Read the specified number of records
    25.  $rs =mysql_query("select * from myTable order by id desc limit $offset,$pagesize",$conn);
    26.  if ($myrow = mysql_fetch_array($rs))
    27.  {
    28.   $i=0;
    29.   ?>
    30.   < ;table border="0" width="80%">
    31.  
  •   

    Title

  •   

    Release time

  • ";
  •  }
  •  echo "
    There are ".$pages." pages (".$page."/".$ pages.")";
  •  for ($i=1;$i< $page;$i++)
  •   echo "[". $i ."] ";
  •  echo "[".$page."]";
  •  for ($i=$page+1;$i<=$pages;$i++)
  •  echo "< ;a href='fenye.php?page=".$i."'>[".$i ."] ";
  •   echo "
  • ";
  •   ?>
  •  
  • Copy code

    5. Summary The code runs normally on windows2003 server+php4.4.0+mysql5.0.16. The paging format shown in this example is [1][2][3]….

    If you need to display it in the form of "Home Page Previous Page Next Page Last Page", the code:

    1. $first=1;
    2. $prev=$page-1;
    3. $next=$page+1;
    4. $last=$pages;
    5. if ($page > 1)
    6. {
    7.  echo " Homepage ";
    8.  echo "Previous page ";
    9. }
    10. if ($page < $pages)
    11. {
    12.  echo "
    13.  echo "Last page ";
    14. }
    Copy code

    The above paging code is relatively simple. It can be used as a reference for getting started. Once you have mastered the principles of PHP paging, it will be much easier to write paging code.

    Recommended reading:



    Related labels:
    source:php.cn
    Previous article:PHP data file cache class code example Next article:PHP pie chart statistics code usage
    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
    Latest Articles by Author
    Latest Issues
    Related Topics
    More>
    Popular Recommendations
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template