[英]php my sql pagination help need for links and advancing the next offset
[英]Php pagination with links range. Need help adjusting the loop
稍有變化
分頁永遠不要顯示超過5個按鈕/鏈接(不計算next / prev),如果我在分頁中間,應該顯示
1 ... 5 6 7 ... 20
要么
1 ... 9 10 11 ... 20
如果在最后一頁
1 ... 17 18 19 20
我從這個開始
function pagination (){
// prev link
$html ='';
$numpages = 20;
$current_page = 1;
$dotshow = true;
if ($numpages != 1) {
$html .='<span><i class="fa fa-angle-left"></i></span>';// prev
for($i=1; $i <= $numpages; $i++){
if ($i == 1 || $i == $numpages || ($i >= $current_page - 3 && $i <= $current_page + 3) ) {
$dotshow = true;
if ($i != $current_page){
$html .='<a class="pagination-link" href="#linkhere">';
$html .='<span> '.$i.'</span>';
$html .='</a>';
}else{
$html .='<span class="current">';
$html .='<span> '.$i.'</span>';
$html .='</span>';
}
}else if ( $dotshow ){
$dotshow = false;
$html .='<span class="dots">';
$html .='<span> ... </span>';
$html .='</span>';
}
}
$html .='<span><i class="fa fa-angle-right"></i></span>';// next
}
if($html !=''){
return $html;
}
}
首先,我得到這個
但是在我的第5頁上卻顯示了這一點,並且由於錯誤的current,total,limit calc導致鏈接數量增加。
任何幫助表示贊賞!
這是架構。 您可以使用html代碼,樣式和變量對其進行調整:
$pages
是總頁數, $page
是當前頁, $start
是“組”頁面的第一頁:
/* Set subgroup page start: */
if ( $pages < 6 ) $start = 2;
elseif( $page < 3 ) $start = 2;
elseif( $page > $pages-3 ) $start = $pages - 3;
else $start = $page - 1;
/* Compose line: */
/* Page 1 (always): */
$output = '[1]';
/* Initial separator: */
if( $start > 2 ) $output .= '...';
/* Page subgroup: ends if reached +2 or pages-1 */
for( $i = $start; $i < $pages; $i++ )
{
$output .= "[$i]";
if( $i > ($start+1) ) break;
}
/* Final separator: */
if( $start < $pages - 3 ) $output .= '...';
/* Last page: */
if( $pages > 1 ) $output .= "[$pages]";
/* Output: */
echo $output;
我在代碼中添加了注釋,因為我認為它是不言自明的。 隨時問您是否有疑問。
Thnx到Fusion3K的解決方法,但我的版本的修復是調整特定頁碼的限制
function pagination (){
$html ='';
$numpages = 20;
$current_page = 1;
$dotshow = true;
if( $current_page == 2 || $current_page == $numpages -1 ){
$limit = 2;
}else if($current_page >= 3 && $current_page != $numpages ){
$limit = 1;
}else{
$limit = 3;
}
if ($numpages != 1) {
$html .='<span><i class="fa fa-angle-left"></i></span>';// prev
for($i=1; $i <= $numpages; $i++){
if ($i == 1 || $i == $numpages || ($i >= $current_page - $limit && $i <= $current_page + $limit) ) {
$dotshow = true;
if ($i != $current_page){
$html .='<a class="pagination-link" href="#linkhere">';
$html .='<span> '.$i.'</span>';
$html .='</a>';
}else{
$html .='<span class="current">';
$html .='<span> '.$i.'</span>';
$html .='</span>';
}
}else if ( $dotshow ){
$dotshow = false;
$html .='<span class="dots">';
$html .='<span> ... </span>';
$html .='</span>';
}
}
$html .='<span><i class="fa fa-angle-right"></i></span>';// next
}
if($html !=''){
return $html;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.