Home > Article > Backend Development > Simple and practical php mysql paging code_PHP tutorial
Simple and practical php mysql paging code
Simple and practical php tutorial mysql tutorial paging code
$qh=mysql_query("select count(*) as rcnt from table where your_condition_here order by whatever");
$data=mysql_fetch_array($qh);
$nr=$data["rcnt"];
//Determine whether the offset parameter is passed to the script, if not, use the default value 0if (empty($offset))
{
$offset=0;
}//Query results (here are 20 items per page, but you can change it yourself)
$result=mysql_query("select id,name,phone from table where your_condition_here order by whatever limit $offset, 20");//Display the 20 records returned
while ($data=mysql_fetch_array($result))
{//Replace with the code you use to display the returned records
}
//Next step, write links to other pages
if(!$offset) //If the offset is 0, the link to the previous page will not be displayed
{
$preoffset=$offset-20;
print "Previous page n";
}//Calculate the total number of pages required
$pages=ceil($nr/20); //$pages variable now contains the required number of pagesfor ($i=1; $i <= $pages; $i++)
{
$newoffset=20*$i;
print "$i n";
}//Check if it is the last page
if ($pages!=0 && ($newoffset/20)!=$pages)
{
print "Next page n";
}