其实就是根据查询的总页先分页后判断,具体的我贴段我经常用的分页代码吧.
function multi($count,$page_size,$curpage,$url,$showkbd=false,$shownum=true,$maxpages = 0, $page = 10, $autogoto = FALSE, $simple = FALSE){
$face=array('limit_start'=>0,'multipage'=>'');
$c0=$count;
$c0 or $c0=1;
$page_size or $page_size=1;
$pnums=@ceil($c0/$page_size);
$curpage=intval($curpage);
$curr_page=min((empty($curpage)? 1 : $curpage),$pnums);
$face['start']=($curr_page-1) * $page_size;
$face['page']=multipage($count,$page_size,$curr_page,$url,$showkbd,$shownum,$maxpages,$page, $autogoto, $simple);
return $face;
}
function multipage($num, $perpage, $curpage, $mpurl, $showkbd,$shownum,$maxpages, $page, $autogoto, $simple) {
global $_INPUT;//自己处理过的包含$_GET和$_POST的数组
$lang=array();
$hasajax = !empty($_INPUT['hasajax']) ? " hasajax=\"".htmlspecialchars($_INPUT['hasajax'])."\" " : '';
$a_name = '';
if(strpos($mpurl, '#') !== FALSE) {
$a_strs = explode('#', $mpurl);
$mpurl = $a_strs[0];
$a_name = '#'.$a_strs[1];
}
$lang['prev'] = '«上一页';
$lang['next'] = '下一页»';
$multipage = '';
$mpurl .= strpos($mpurl, '?') !== FALSE ? '&' : '?';
$realpages = $pages=1;
$_INPUT['page_next'] = 0;
$page -= strlen($curpage) - 1;
if($page <= 0) {
$page = 1;
}
if($num > $perpage) {
$offset = floor($page * 0.5);
$realpages = @ceil($num / $perpage);
$pages = $maxpages && $maxpages < $realpages ? $maxpages : $realpages;
if($page > $pages) {
$from = 1;
$to = $pages;
} else {
$from = $curpage - $offset;
$to = $from + $page - 1;
if($from < 1) {
$to = $curpage + 1 - $from;
$from = 1;
if($to - $from < $page) {
$to = $page;
}
} elseif($to > $pages) {
$from = $pages - $page + 1;
$to = $pages;
}
}
$_INPUT['page_next'] = $to;
$multipage = ($curpage > 1 ? ''.$lang['prev'].'' : '').($curpage - $offset > 1 && $pages > $page ? '1 … ' : '');
for($i = $from; $i <= $to; $i++) {
$multipage .= $i == $curpage ? ''.$i.'' :
''.$i.'';
}
$multipage .= ($to < $pages ? ' … '.$realpages.'' : '').
($curpage < $pages && !$simple ? ''.$lang['next'].'' : '').
($showkbd && !$simple && $pages > $page && !$hasajax ? ' 转到' : '');
$multipage = $multipage ? ''.($shownum && !$simple ? '共'.$num.'条 第'.$curpage.'/'.$pages.'页' : '').$multipage.'' : '';
}
$maxpage = $realpages;
if(empty($multipage)){$multipage =''.($shownum && !$simple ? '共'.$num.'条 第'.$curpage.'/'.$pages.'页' : '').'1';}
return $multipage;
}
调用:
$multipage=multi(总条数, 每页行数, 当前页, 页码的URL(不含页码));
各参数你自己提供.