PHP动态分页函数,PHP开发分页必备啦

时间:2022-01-20 01:46:50

贴代码: 

复制代码代码如下:


/** 
* 分页函数 

* @param int $count 条目总数 
* @param int $perlogs 每页显示条数目 
* @param int $page 当前页码 
* @param string $url 页码的地址 
*/ 
function pagination($count,$perlogs,$page,$url,$anchor=''){ 
$pnums = @ceil($count / $perlogs); 
$re = ''; 
$urlHome = preg_replace("|[\?&/][^\./\?&=]*page[=/\-]|","",$url); 
for ($i = $page-5;$i <= $page+5 && $i <= $pnums; $i++){ 
if ($i > 0){ 
if ($i == $page){ 
$re .= " <span>$i</span> "; 
} elseif($i == 1) { 
$re .= " <a href=\"$urlHome$anchor\">$i</a> "; 
} else { 
$re .= " <a href=\"$url$i$anchor\">$i</a> "; 



if ($page > 6) $re = "<a href=\"{$urlHome}$anchor\" title=\"首页\">«</a><em>...</em>$re"; 
if ($page + 5 < $pnums) $re .= "<em>...</em> <a href=\"$url$pnums$anchor\" title=\"尾页\">»</a>"; 
if ($pnums <= 1) $re = ''; 
return $re;