<?php
/**
* 分页函数
* @param type $num 数据总数
* @param type $perpage 每页总数
* @param type $curpage 当前分页
* @param type $mpurl 路径
* @param type $maxpages 最大分页
* @param type $page
* @param type $autogoto
* @param type $simple
* @return type
*/
function multi($num, $perpage, $curpage, $mpurl, $maxpages = 0, $page = 10, $autogoto = FALSE, $simple = FALSE) {
$ajaxtarget = ‘‘;
$shownum = $showkbd = FALSE;
$prev_page = ‘上一页‘;
$next_page = ‘下一页‘;
//URL分析:
$mpurl = empty($mpurl) ? $_SERVER[‘REQUEST_URI‘] : $mpurl;
$parse_url = parse_url($mpurl);
//单独取出URL的查询字串
if ($parse_url["path"] && $parse_url["query"]) {
$url = $url_query = $parse_url["query"];
} else {
$url = $url_query = $parse_url["path"];
}
if ($url_query) {
//因为URL中可能包含了页码信息,我们要把它去掉,以便加入新的页码信息。
$url_query = preg_replace("/((^|&)page=$curpage|page\/$curpage)/i", ‘‘, $url_query); //兼容path_info 模式
//将处理后的URL的查询字串替换原来的URL的查询字串:
$mpurl = str_replace($url, $url_query, $mpurl);
}
$a_name = ‘‘;
if (strpos($mpurl, ‘#‘) !== FALSE) {
$a_strs = explode(‘#‘, $mpurl);
$mpurl = $a_strs[0];
$a_name = ‘#‘ . $a_strs[1];
}
$mpurl .= strpos($mpurl, ‘?‘) !== FALSE ? ‘&‘ : ‘?‘;
$realpages = 1;
$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;
}
}
$multipage = ‘‘;
$multipage = ($curpage - $offset > 1 && $pages > $page ? ‘<a href="‘ . $mpurl . ‘page=1‘ . $a_name . ‘" class="first"‘ . $ajaxtarget . ‘>首页</a>‘ : ‘‘) .
($curpage > 1 && !$simple ? ‘<a href="‘ . $mpurl . ‘page=‘ . ($curpage - 1) . $a_name . ‘" class="pages-pre"‘ . $ajaxtarget . ‘>‘ . $prev_page . ‘</a>‘ : ‘‘);
for ($i = $from; $i <= $to; $i++) {
$multipage .= $i == $curpage ? ‘<strong>‘ . $i . ‘</strong>‘ :
‘<a href="‘ . $mpurl . ‘page=‘ . $i . ($ajaxtarget && $i == $pages && $autogoto ? ‘#‘ : $a_name) . ‘"‘ . $ajaxtarget . ‘>‘ . $i . ‘</a>‘;
}
$multipage .= ($curpage < $pages && !$simple ? ‘<a href="‘ . $mpurl . ‘page=‘ . ($curpage + 1) . $a_name . ‘" class="pages-next"‘ . $ajaxtarget . ‘>‘ . $next_page . ‘</a>‘ : ‘‘) .
($to < $pages ? ‘<a href="‘ . $mpurl . ‘page=‘ . $pages . $a_name . ‘" class="last"‘ . $ajaxtarget . ‘>末页</a>‘ : ‘‘) .
($showkbd && !$simple && $pages > $page && !$ajaxtarget ? ‘<kbd><input type="text" name="custompage" size="3" onkeydown="if(event.keyCode==13) {window.location=\‘‘ . $mpurl . ‘page=\‘+this.value; doane(event);}" /></kbd>‘ : ‘‘);
$multipage = $multipage ? ‘<div class="pages fr">‘ . ($shownum && !$simple ? ‘<em> ‘ . $num . ‘ </em>‘ : ‘‘) . $multipage . ‘</div>‘ : ‘‘;
}
$maxpage = $realpages;
return $multipage;
}
?>