首页 > 开发 > PHP > 正文

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

2024-05-04 23:05:55
字体:
来源:转载
供稿:网友

参考代码如下:

 

/*** 获取页码导航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."]条数据";}}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表