Home > Backend Development > PHP Tutorial > PHP+MySQL paging display record code

PHP+MySQL paging display record code

WBOY
Release: 2016-07-25 08:54:06
Original
967 people have browsed it
  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. //Judge 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 the code

3. Create the use case table myTable

  1. create table myTable(id int NOT NULL auto_increment,
  2. news_title varchar(50),
  3. news_cont text,
  4. add_time datetime,
  5. PRIMARY KEY(id))
Copy code

4. Complete code

  1. php paging example_bbs.it-home.org
  2.  $conn=mysql_connect("localhost","root","");
  3.  //Set the number of records displayed on each 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'] ; Specify the number of records
  17.  $rs=mysql_query("select * from myTable order by id desc limit $offset,$pagesize",$conn);
  18.  if ($myrow = mysql_fetch_array($rs))
  19.  {
  20.  $i=0;
  21. ?>
  22.  
  23.  
  24.   
  25.   
  26.   
  27.   
  28.     }
  29. while ($myrow = mysql_fetch_array($rs));
  30.    echo "
  31.   

  32.     do {
  33.    $i++;
  34.   ?>
  35.   
  36. ";
  37.  }
  38.   echo "
    There are ".$pages." pages (".$page ."/".$pages.")";
  39.  for ($i=1;$i< $page;$i++)
  40.  echo "[".$i ."] ";
  41.   Echo "[".$page."]";
  42.   for ($i=$page+1;$i<=$pages;$ i++)
  43.  echo "[".$i ."] ";
  44.  echo "
" ;
  •   ?>
  • 5. Summary

  • This example code runs normally on windows2000 server+php4.4.0+mysql5.0.16.
  • The displayed paging format is [1][2][3]... this way.
  • If you want to display it as "Home Page Previous Page Next Page Last Page", please add the code:
  • $first=1;
  • $prev=$page-1;
  • $next=$page+1;
  • $last= $pages;

  • if ($page > 1)

  • {
  •  echo "Homepage< /a> ";
  •  echo "Previous page ";
  • }

  • {
  •  echo "next page
  •  echo "Last page ";
  • }

  • Copy code
  • In fact, the code for writing paging display is It's very simple, just understand how it works
  • .
  • 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