Home  >  Article  >  Backend Development  >  Detailed explanation of paging, worry-free paging with PHP+mysql

Detailed explanation of paging, worry-free paging with PHP+mysql

WBOY
WBOYOriginal
2016-07-29 08:37:171006browse

All the codes I posted are original and have been used in multiple projects. I often use php+mysql paging code

Copy the code The code is as follows:


$perpagenum = 10;/ /Define how many pieces of data are displayed on each page
$total = mysql_fetch_array(mysql_query("select count(*) from a"));//Query how many pieces of data there are in the database
$Total = $total[0]; $Totalpage = ceil($Total/$perpagenum);//Round up, round
if(!isset($_GET['page'])||!intval($_GET['page'])||$_GET ['page']>$Totalpage)//Four possible states of page
{
  $page=1;
}
else
{
  $page=$_GET['page'];//If the above is not met In four cases, the value of page is $_GET['page']
}
$startnum = ($page-1)*$perpagenum;//Starting number
$sql = "select * from a order by id limit $startnum,$perpagenum";//Query the required number of items
echo $sql."
";
$rs = mysql_query($sql);
$contents = mysql_fetch_array($rs);
if($total) If $ Total is not empty, execute the following statements
{
Do
{
$ ID = $ Contents ['ID'];
$ Name = $ Contents ['name'];
? "0" align="center">
                                                                                                                                            }
while($contents = mysql_fetch_array($rs));//do....while
$per = $page - 1;//Previous page
$next = $page + 1;//Next page
echo "if($page != 1)
{
echo "Homepage";
echo " Previous page";
}
if($page != $Totalpage)
{
echo " Next page";
echo " Last page";
}
}
else If $total is empty, output No message
{
echo "

No message
";
}
?>


The above has introduced the detailed explanation of paging. From now on, pagination will be worry-free with PHP+mysql, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.


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