php下封装较好的数字分页方法

作者:网络 来源:佚名 更新时间:2010-11-24 17:18:13 点击:

参考代码如下:

 

/**
* 获取页码导航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."]条数据";
}
}