Another PHP paging class implementation code
- 2020-03-31 17:05:42
- OfStack
<?php
function genpage(&$sql,$page_size=10)
{
global $pages,$sums,$eachpage,$page; //Total pages, total records, per page, current page
$page = $_GET["page"];
if($page ==0)$page =1;
$eachpage = $page_size;
$pagesql = strstr($sql," from ");
$pagesql = "select count(*) as ids ".$pagesql;
$conn = mysql_query($pagesql) or die(mysql_error());
if($rs = mysql_fetch_array($conn))$sums = $rs[0];
$pages=ceil($sums/$eachpage);
if($pages==0)$pages=1;
$startpos = ($page-1)*$eachpage;
$sql .=" limit $startpos,$eachpage ";
}
//According to the paging
function showpage()
{
global $pages,$sums,$eachpage,$page; //Total pages, total records, per page, current page, other parameters
$link=$_SERVER['PHP_SELF'];
echo " record ".$sums.":".$eachpage." ";
echo " Number of pages ".$page."/".$pages." ";
$p_head=$page-5;
if($p_head<=0)$p_head=1; //The page number cycle begins with the first five
$p_end=$page+5;
if($p_end>$pages)$p_end=$pages; //5 after the end of the page number cycle
echo "[<a href=$link?page=1> Home page </a>] ";
for($i=$p_head;$i<=$p_end;$i++)
{
if($i!=$page)
echo "<a href=$link?page=$i>[$i]</a> ";
else
echo "<b><strike>[$i]</strike></b> ";
}
echo " [<a href=$link?page=$pages> At the end of the page </a>]";
}
?>