Home > Article > Backend Development > PHP encapsulates better digital paging method class_PHP tutorial
/**
* Get page navigation HTML
* @param $pageNum: current page number
* @param $pageSize: Number of pages per page
* @param $rowCount: total number of records
* @param $navUrl: Link page URL
*/
Function getNavHtml($pageNum,$pageSize,$rowCount,$navUrl){
$pageCount = (int)($rowCount/$pageSize); //Total number of pages
if ($rowCount % $pageSize >0){
$pageCount++;
}
if ($pageNum>$pageCount){
$pageNum = 1;
}
$firstNav = "Home";
$lastNav = "Last page";
$prevNav="";
$nextNav="";
if ($pageNum>1){
$navPageNum = $pageNum-1;
$prevNav = "Previous page";
}
if ($pageNum<$pageCount && $pageCount>1){
$navPageNum = $pageNum+1;
$nextNav = "Next page";
}
$amongNav="";//Key loop
for ($i=1;$i<=5;$i++){
$navPageNum = $pageNum+ $i-3;
if ($navPageNum>0 && $navPageNum<=$pageCount){
$navCSS = $navPageNum == $pageNum?" class="hover"":"";
$amongNav.="{$navPageNum} ";
}
}
Return $firstNav.$prevNav.$amongNav.$nextNav.$lastNav." ".$pageNum."/".$pageCount." There are [".$rowCount."] pieces of data";
}
}