2017-04-07 73 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