php下封裝較好的數字分頁方法
來源:互聯網
上載者:User
複製代碼 代碼如下:
/**
* 擷取頁碼導航HTML
* @param $pageNum:當前頁碼
* @param $pageSize:每頁數量
* @param $rowCount:記錄總數
* @param $navUrl:連結頁面URL
*/
function getNavHtml($pageNum,$pageSize,$rowCount,$navUrl){
$pageCount = (int)($rowCount/$pageSize); //總頁數
if ($rowCount % $pageSize >0){
$pageCount++;
}
if ($pageNum>$pageCount){
$pageNum = 1;
}
$firstNav = "<a href=\"{$navUrl}page=1\">首頁</a> ";
$lastNav = "<a href=\"{$navUrl}page={$pageCount}\">尾頁</a> ";
$prevNav="";
$nextNav="";
if ($pageNum>1){
$navPageNum = $pageNum-1;
$prevNav = "<a href=\"{$navUrl}page={$navPageNum}\">上一頁</a> ";
}
if ($pageNum<$pageCount && $pageCount>1){
$navPageNum = $pageNum+1;
$nextNav = "<a href=\"{$navUrl}page={$navPageNum}\">下一頁</a> ";
}
$amongNav="";
//關鍵迴圈
for ($i=1;$i<=5;$i++){
$navPageNum = $pageNum+ $i-3;
if ($navPageNum>0 && $navPageNum<=$pageCount){
$navCss = $navPageNum == $pageNum?" class=\"hover\"":"";
$amongNav.="<a href=\"{$navUrl}page={$navPageNum}\" $navCss>{$navPageNum}</a> ";
}
}
return $firstNav.$prevNav.$amongNav.$nextNav.$lastNav." ".$pageNum."/".$pageCount." 共有[".$rowCount."]條資料";
}
}