代码:
$ppp = $keyword ? $keyword : 20; //一页显示数目
$page = empty($page) || !ispage($page) ? 1 : $page;
$start_limit = $numpost =($page - 1) * $ppp;
//分页
$query = $db->query("SELECT COUNT(*) as total FROM {$tablepre}{$table}
WHERE $condition");//获取该类别的记录总数
$result = $db->fetch_array($query);
$multipage = setpage("list.php?action=$action&page={n}",$page,@ceil
($result['total'] / $ppp));
//end
$sql = "SELECT $dotadd FROM {$tablepre}{$table} t WHERE $condition
$groupby $orderby LIMIT $start_limit, $ppp";
调用函数:
//分页
function pageItemList($cp,$url,$n,$sF,$sN,$tgt=''){
$target = $tgt ? ' target="'.$tgt.'"' : '';
$sF = ($sF<=0) ? 1 : $sF;
$sN = ($sN>$n) ? $n : $sN;
$page_pre = str_replace('{n}',($cp-1),$url);
$page_next = str_replace('{n}',($cp+1),$url);
if($cp<=1){
$rtv .= '<span>上一页</span>';
} else {
$rtv .= '<a href="'.$page_pre.'"'.$target.'>上一页</a>';
}
for($i=$sF;$i<=$sN;$i++){
if($cp==$i){
$rtv .= '<span class="s">'.$i.'</span>';
} else {
$lct=str_replace('{n}',$i,$url);
$rtv .= '<a href="'.$lct.'"'.$target.'>'.$i.'</a>';
}
}
if($cp>=$n){
$rtv .= '<span>下一页</span>';
} else {
$rtv .= '<a href="'.$page_next.'"'.$target.'>下一页</a>';
}
return $rtv;
}
function setpage($url,$cp,$n,$tgt=''){
if(intval($n)==0)return;
$target = $tgt ? ' target="'.$tgt.'"' : '';
$page_first = str_replace('{n}','1',$url);
$page_last = str_replace('{n}',$n,$url);
if($cp<=1){
$rtv .= '<span>第一页</span>';
} else {
$rtv .= '<a href="'.$page_first.'"'.$target.'>第一页</a>';
}
if($n>=10){
if($cp<=4){
$rtv .= $cp>1 ? pageItemList($cp,$url,$n,1,10,$tgt) :
pageItemList($cp,$url,$n,1,($cp+9),$tgt);
} else {
$rtv .= pageItemList($cp,$url,$n,($cp-4),($cp+5),$tgt);
}
} else {
$rtv .= pageItemList($cp,$url,$n,1,$n,$tgt);
}
if($cp>=$n){
$rtv .= '<span>最后一页</span>';
}else{
$rtv .= '<a href="'.$page_last.'"'.$target.'>最后一页</a>';
}
return $rtv;
}
样式:
.morepage {float:right;line-height:23px;text-align:right;}
.morepage a,.morepage span {float:left;padding:0 5px;margin:0 4px;border:1px
solid #999;background-color:#f2f2f2;font-size:14px;font-family:Arial;}
.morepage a.s,.morepage span.s {background-color:#f60;font-
weight:bold;color:#fff;border:1px solid #f60;}
.morepage a {text-decoration:underline;}
调用:
<div class="morepage">
$multipage
</div>