2017-04-07 83 views
0

我有一些分頁問題。我的分頁工作正常。但是我想顯示開始的第3頁後,我想顯示(....),最後我想顯示我的最後一頁。這裏是我的代碼:Display ... in分頁

<?php 
$videocount= 1000; 
$page = (!isset($_GET['page']))? 1 : $_GET['page']; 
$prev = ($page - 1); 
$next = ($page + 1); 

/* Max results per page */ 
$max_results = 50; 

/* Calculate the offset */ 
$from = (($page * $max_results) - $max_results); 
$last = $from + $max_results; 

/* Query the Api for total results.*/ 
//$total=107; 
$total_results = $videocount; 
$total_pages = ceil($total_results/$max_results); 
if($page==$total_pages){ 
    $last=$total_results ; 
}else{ 
    $last=$last;  
} 

$pagination = ''; 
/* Create a PREV link if there is one */ 
if($page > 1) { 
    $pagination.= '<li> <a href="?&page='.$prev.'"> <span class="prevBtn"><i class="fa fa-caret-left"></i> Previous</span> </a></li>'; 
} 
/* Loop through the total pages */ 
//for($i = 1; $i <= $total_pages; $i++) before meeting code 
for($i = 1; $i <= $total_pages; $i++) { 
    if(($page) == $i) { 
     //$pagination .= $i; 
     $pagination .= '<li> <a href="#"> <span>'.$i.' </span> </a></li>'; //implement active class here 
    } else { 
     $pagination .= '<li> <a href="?page='.$i.'"> <span> '.$i.' </span> </a></li>'; 
    } 
} 

if($page < $total_pages) { 
    $pagination.= '<li> <a href="?page='.$next.'"> <span class="nextBtn"> Next <i class="fa fa-caret-right"></i> </span> </a></li>'; 
} 
?> 

感謝,

回答

1

我已經在你的代碼中添加環內一個if ... else語句。這可以幫助你

<?php 
$videocount= 1000; 
$page = (!isset($_GET['page']))? 1 : $_GET['page']; 
$prev = ($page - 1); 
$next = ($page + 1); 
/* Max results per page */ 
$max_results = 50; 
/* Calculate the offset */ 
$from = (($page * $max_results) - $max_results); 
$last=$from + $max_results; 
/* Query the Api for total results.*/ 
//$total=107; 
$total_results = $videocount; 
$total_pages = ceil($total_results/$max_results); 
if($page==$total_pages) { 
    $last=$total_results ; 
} else { 
    $last=$last;  
} 
$pagination = ''; 

/* Create a PREV link if there is one */ 
if($page > 1) { 
    $pagination.= '<li> <a href="?&page='.$prev.'"> <span class="prevBtn"><i class="fa fa-caret-left"></i> Previous</span> </a></li>'; 
} 

/* Loop through the total pages */ 
//for($i = 1; $i <= $total_pages; $i++) before meeting code 
for($i = 1; $i <= $total_pages; $i++) { 
    if(($page) == $i) { 
     //$pagination .= $i; 
     $pagination .= '<li> <a href="#"> <span>'.$i.' </span> </a></li>';  //implement active class here 
    } else { 
     //Display first 3 pages 
     if($i<=3) 
      $pagination .= '<li> <a href="?page='.$i.'"> <span> '.$i.' </span> </a></li>'; 
     //Display last 3 pages 
     else if($total_pages-$i<3) 
      $pagination .= '<li> <a href="?page='.$i.'"> <span> '.$i.' </span> </a></li>'; 
     else { 
      if($total_pages-$i<=6) 
       $pagination .= '<li> <a href="?page='.$i.'"> <span> . </span> </a></li>'; 
     } 
    } 
} 
if($page < $total_pages) { 
    $pagination.= '<li> <a href="?page='.$next.'"> <span class="nextBtn"> Next <i class="fa fa-caret-right"></i> </span> </a></li>'; 
} 
echo $pagination; 
?> 
+0

「試試這個」不是的什麼是錯的一個很好的描述,你在哪裏解決這一問題的解決方案。也許一些符號是有序的。 – Rasclatt

+0

感謝您的迴應您的先生....您的代碼格式是1,2,3 ...... 18,19,20但我想要的格式是1,2,3 ...... 20 –

+0

@Rasclatt感謝您的建議,我已更正說明 – ManiMuthuPandi