在一個網(wǎng)站中,分布必不可少,如何巧妙的組織一個可控性強(qiáng)的分頁函數(shù)顯得猶為重要更卒,前面在模型類中已經(jīng)把它交給了函數(shù)multipage,這里稚照,對它進(jìn)行完善
function multipage($page, $page_num, $url, $isajax = 0) {
$count = count($url);
if ($isajax) {
$css = " class='__ajax' action=\\".__page-box\\" ";
} else {
$css = '';
}
//html開始
$html = '<ul class="am-pagination pagination">';
//構(gòu)造上一頁
if ($page == 1) {
$html .= "<li class='am-active active'><a href='javascript:;>< </a></li>";
}else {
$pre = $page - 1;
$url[$count] = $pre;
$html .= "<li><a $css title=\\"上一頁\\" href='" . implode('.', $url) . "'><</a></li>";
}
//構(gòu)造第一頁
if ($page == 1) {
$html .= "<li class='am-active active'><a href='javascript:;'>1</a></li>";
} else {
$url[$count] = 1;
$html .= "<li><a $css href='" . implode('.', $url) . "'>1</a></li>";
}
//構(gòu)造顯示中間的頁碼
if (1) {
$arr = range($page-1, $page + 2, 1);
} else {
$arr = range(2, 3, 1);
$tmp1 = array_merge(range($page - 2, $page + 2, 1) , range($page_num - 2, $page_num + 1, 1));
$arr = array_unique(array_merge($arr, $tmpl));
}
$j = 1;
foreach ($arr as $i) {
if ($page_num < $i || $i < 2) {
continue;
}
if ($j > 0 and $i - $j > 1) {
$html .= "<li><a class='nolink'>...</a></li>";
}
if ($page == $i) {
$html .= "<li class='am-active active'><a href='javascript:;'>$i</a></li>";
} else {
$url[$count] = $i;
$html .= "<li><a $css href='" . implode('.', $url) . "'>$i</a></li>";
}
$j = $i;
}
if ($page_num - $i > 1) {
$html .= "<li><a class='nolink'>...</a></li>";
$url[$count] = $page_num;
$html .= "<li><a href='" . implode('.', $url) . "'>$page_num</a></li>";
} elseif ($page_num - $i == 1) {
$url[$count] = $page_num;
$html .= "<li><a $css href='" . implode('.', $url) . "'>$page_num</a></li>";
}
if ($page == $page_num) {
$html .= "<li class='am-active active'><a href='javascript:;'>></a></li>";
} else {
$next = $page + 1;
$url[$count] = $next;
$html .= "<li><a $css href='" . implode('.', $url) . "' title='下一頁'>></a></li>";
}
$html .= "</ul>";
echo $html;
}