SoFunction
Updated on 2025-03-02

Detailed explanation of php+mysql paging code


<?php   
$perpagenum = 10;//Define how many items are displayed per page
$total = mysql_fetch_array(mysql_query("select count(*) from a"));//Query how many pieces of data are there in the database
$Total = $total[0];                          //   
$Totalpage = ceil($Total/$perpagenum);//Put it, round it
if(!isset($_GET['page'])||!intval($_GET['page'])||$_GET['page']>$Totalpage)//page possible four states
{   
    $page=1;   
}   
else   
{   
$page=$_GET['page'];//If the above four situations are not met, the value of page is $_GET['page']
}   
$startnum    = ($page-1)*$perpagenum;//The number of starts
$sql = "select * from a order by id limit $startnum,$perpagenum";//Query the required number of entries
echo $sql."   
";   
$rs = mysql_query($sql);   
$contents = mysql_fetch_array($rs);   
if($total) If $total is not empty, execute the following statement
{   
    do   
    {   
    $id = $contents['id'];   
    $name = $contents['name'];   
    ?>   
    <table border="0" align="center">   
    <tr>   
    <td>id:   
    <?php echo $id;?>   
    </td>   
    </tr>   
    <tr>   
    <td>name:   
    <?php echo $name;?>   
    </td>   
    </tr>   
    </table>   
    <?php   
    }   
while($contents = mysql_fetch_array($rs));//do....while   
$per = $page - 1;//Previous page
$next = $page + 1;//Next page
echo "<center> has a total of".$Total." records, per page".$perpagenum." entries, a total of".$Totalpage." pages";
if($page != 1)   
{   
echo "<a href='".$_SERVER['PHP_SELF']."'>Home</a>";
echo "<a href='".$_SERVER['PHP_SELF'].'?page='.$per."'> Previous page</a>";
}   
if($page != $Totalpage)   
{   
echo "<a href='".$_SERVER['PHP_SELF'].'?page='.$next."'> Next page</a>";
echo "<a href='".$_SERVER['PHP_SELF'].'?page='.$Totalpage."'> Last Page</a></center>";
}   
}   
else if $total is empty, output No message
{   
echo "<center>No message</center>";   
}   
?>