在分页中显示...

时间:2017-04-07 05:47:46

标签: php

我有一些分页问题。我的分页工作很好。但是我希望在显示(....)之后显示已启动的3页,最后一点我想显示我的最后一页。这是我的代码:

<?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 个答案:

答案 0 :(得分: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;
?>