public static function getpageHTML($url, $currentPage, $pageCount, $neighborLength = 3){
if($pageCount < 2 )
return "";
$start = $currentPage - $neighborLength;
$end = $currentPage + $neighborLength;
if($start <= 4){
$start = 2;
}
$start = $start > 1 ? $start : 2;
$end = 2* $neighborLength - ($start < $currentPage? ($currentPage - $start ) : 0 ) + $currentPage;
$end = $end < $pageCount ? $end : ( $pageCount - 1 );
$str = "";
//上一页
$str.= $currentPage == 1 ? '<span class="page-start"><b><</b>上一页</span>' : '<a href="' . str_replace("{page}", $currentPage - 1, $url) . '" class="page-prev"><b><</b>上一页</a>';
//第一页
$str.= $currentPage == 1 ? "" : '<a href="' . str_replace("{page}", 1, $url) . '">1</a>';
//左邻居
if( $start != 2 )
$str.= '<span class="page-break">...</span>';
for($i = $start; $i < $currentPage; $i++){
$str.= '<a href="' . str_replace("{page}", $i, $url) . '">' . $i . '</a>';
}
//当前页
$str.= '<span class="page-this">' . $currentPage . '</span>';
//右邻居
for($i = $currentPage + 1; $i < $end + 1; $i++){
$str.= '<a href="' . str_replace("{page}", $i, $url) . '">' . $i . '</a>';
}
if( $end != $pageCount - 1 )
$str.= '<span class="page-break">...</span>';
//最后一页
$str.= $currentPage != $pageCount ? '<a href="' . str_replace("{page}", $pageCount, $url) . '">' . $pageCount . '</a>' : '';
//下一页
$str.= $currentPage != $pageCount ? '<a href="' . str_replace("{page}", $currentPage + 1, $url) . '" class="page-next">下一页<b>></b></a>' : '<span class="page-end">下一页<b>></b></span>';
//输入框跳转
$str.= '<span class="page-skip"> 到第<input type="text" value="' . $currentPage . '" maxlength="3">页<button value="go" οnclick="var a=parseInt($(this).parent().find(\'input[type=text]\').val(),10);a=(!!a&&a>0&&a<='. $pageCount .')?a:1;window.location.href=\''.str_replace("{page}", '\'+a+\'', $url).'\'" class="button">确定</button></span>';
return $str;
if($pageCount < 2 )
return "";
$start = $currentPage - $neighborLength;
$end = $currentPage + $neighborLength;
if($start <= 4){
$start = 2;
}
$start = $start > 1 ? $start : 2;
$end = 2* $neighborLength - ($start < $currentPage? ($currentPage - $start ) : 0 ) + $currentPage;
$end = $end < $pageCount ? $end : ( $pageCount - 1 );
$str = "";
//上一页
$str.= $currentPage == 1 ? '<span class="page-start"><b><</b>上一页</span>' : '<a href="' . str_replace("{page}", $currentPage - 1, $url) . '" class="page-prev"><b><</b>上一页</a>';
//第一页
$str.= $currentPage == 1 ? "" : '<a href="' . str_replace("{page}", 1, $url) . '">1</a>';
//左邻居
if( $start != 2 )
$str.= '<span class="page-break">...</span>';
for($i = $start; $i < $currentPage; $i++){
$str.= '<a href="' . str_replace("{page}", $i, $url) . '">' . $i . '</a>';
}
//当前页
$str.= '<span class="page-this">' . $currentPage . '</span>';
//右邻居
for($i = $currentPage + 1; $i < $end + 1; $i++){
$str.= '<a href="' . str_replace("{page}", $i, $url) . '">' . $i . '</a>';
}
if( $end != $pageCount - 1 )
$str.= '<span class="page-break">...</span>';
//最后一页
$str.= $currentPage != $pageCount ? '<a href="' . str_replace("{page}", $pageCount, $url) . '">' . $pageCount . '</a>' : '';
//下一页
$str.= $currentPage != $pageCount ? '<a href="' . str_replace("{page}", $currentPage + 1, $url) . '" class="page-next">下一页<b>></b></a>' : '<span class="page-end">下一页<b>></b></span>';
//输入框跳转
$str.= '<span class="page-skip"> 到第<input type="text" value="' . $currentPage . '" maxlength="3">页<button value="go" οnclick="var a=parseInt($(this).parent().find(\'input[type=text]\').val(),10);a=(!!a&&a>0&&a<='. $pageCount .')?a:1;window.location.href=\''.str_replace("{page}", '\'+a+\'', $url).'\'" class="button">确定</button></span>';
return $str;
}
调用事例:
getpageHTML("/index.php?cid={$categoryId}&page={page}", $currentPage, ceil( $RECORDCount/ $PAGESIZE), (ceil( $RECORDCount / $PAGESIZE) + 1) ),