PHP Pagination show first two pages and last two p

2019-08-03 15:19发布

问题:

I'm currently using the php pagination script below. The pagination displays six results per page with previous/next links. I was wondering if anyone might know what I could add so that the pagination also displays links to the first two pages and the last two pages? Like the pagination in this example page: http://www.winelog.net/wines/Oregon

$data=file("data.txt");
$pages=0;
foreach($data as $temp){
    $x=explode("|",$temp);
    if($x[0] > 0){
        $pages=$pages+1;
    }
}

if($_GET['p']){
    $page=$_GET['p'];
}

if($_GET['i']){
    $index=$_GET['i'];
}

if($index == "p"){
    $page=$page-1;
}
if($index == "n"){
    $page=$page+1;
}
if($page < 1){
    $page=1;
}
if($page  >  $pages){
    $page=$pages;
}
$line=$data[$page-1];
$fields=explode("|",$line);

The displayed navigation:

$show=6;
echo "<li><a href='?i=p&p=$page'>&#171; PREV</li></a>";

if($page-($show/2)  >  1){
    $temp=$page-$show;
    echo "<li><a href='?p=$temp'>...</li></a>";
}

if($page-($show/2) >= 1 && $page+($show/2) <= $pages){
    $start=$page-($show/2);
    $stop=$page+($show/2);
}

if($page-($show/2) < 1){
    $start=1;
    $stop=$show;
}

if($page+($show/2) > $pages){
    $start=$pages-$show;
    $stop=$pages;
}

for($i=$start; $i<=$stop; $i++){
    if($page==$i){
        echo "<li class='active'>$i</li></a>";
    }
    else{
        echo "<li><a href='?p=$i'>$i</li></a>";
    }
}

if($page+($show/2) < $pages){
    $temp=$page+$show;
    echo "<li><a href='?p=$temp'>...</li></a>";
}
echo "<li><a href='?i=n&p=$page'>NEXT &#187;</li></a>";

回答1:

Here's what you need to do.

First get a count of the number of 'pages' that there are.
If there are less than, say, 10 pages - output all the pages.

Otherwise: Output from current page - 5 to current page + 5.
Before the output, put a 'First' button - page = 1
After the output, put a 'Last' button - page = total number of pages.

If you want a second-to-last button, just go page = total number of pages - 1, etc.

You might want to look into Zend_Paginator - You don't need to use the whole Zend Framework to use individual parts of it, it's designed so it can be pulled apart.



回答2:

The full code of their pagination script is available for free @Stranger Studios There is a php and a Perl script available for download.

Code PHP version:

<?php
//function to return the pagination string
function getPaginationString($page = 1, $totalitems, $limit = 15, $adjacents = 1, $targetpage = "/", $pagestring = "?page=")
{       
    //defaults
    if(!$adjacents) $adjacents = 1;
    if(!$limit) $limit = 15;
    if(!$page) $page = 1;
    if(!$targetpage) $targetpage = "/";

    //other vars
    $prev = $page - 1;                                  //previous page is page - 1
    $next = $page + 1;                                  //next page is page + 1
    $lastpage = ceil($totalitems / $limit);             //lastpage is = total items / items per page, rounded up.
    $lpm1 = $lastpage - 1;                              //last page minus 1

    /* 
        Now we apply our rules and draw the pagination object. 
        We're actually saving the code to a variable in case we want to draw it more than once.
    */
    $pagination = "";
    if($lastpage > 1)
    {   
        $pagination .= "<div class=\"pagination\"";
        if($margin || $padding)
        {
            $pagination .= " style=\"";
            if($margin)
                $pagination .= "margin: $margin;";
            if($padding)
                $pagination .= "padding: $padding;";
            $pagination .= "\"";
        }
        $pagination .= ">";

        //previous button
        if ($page > 1) 
            $pagination .= "<a href=\"$targetpage$pagestring$prev\">� prev</a>";
        else
            $pagination .= "<span class=\"disabled\">� prev</span>";    

        //pages 
        if ($lastpage < 7 + ($adjacents * 2))   //not enough pages to bother breaking it up
        {   
            for ($counter = 1; $counter <= $lastpage; $counter++)
            {
                if ($counter == $page)
                    $pagination .= "<span class=\"current\">$counter</span>";
                else
                    $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";                 
            }
        }
        elseif($lastpage >= 7 + ($adjacents * 2))   //enough pages to hide some
        {
            //close to beginning; only hide later pages
            if($page < 1 + ($adjacents * 3))        
            {
                for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                {
                    if ($counter == $page)
                        $pagination .= "<span class=\"current\">$counter</span>";
                    else
                        $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";                 
                }
                $pagination .= "<span class=\"elipses\">...</span>";
                $pagination .= "<a href=\"" . $targetpage . $pagestring . $lpm1 . "\">$lpm1</a>";
                $pagination .= "<a href=\"" . $targetpage . $pagestring . $lastpage . "\">$lastpage</a>";       
            }
            //in middle; hide some front and some back
            elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
            {
                $pagination .= "<a href=\"" . $targetpage . $pagestring . "1\">1</a>";
                $pagination .= "<a href=\"" . $targetpage . $pagestring . "2\">2</a>";
                $pagination .= "<span class=\"elipses\">...</span>";
                for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
                {
                    if ($counter == $page)
                        $pagination .= "<span class=\"current\">$counter</span>";
                    else
                        $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";                 
                }
                $pagination .= "...";
                $pagination .= "<a href=\"" . $targetpage . $pagestring . $lpm1 . "\">$lpm1</a>";
                $pagination .= "<a href=\"" . $targetpage . $pagestring . $lastpage . "\">$lastpage</a>";       
            }
            //close to end; only hide early pages
            else
            {
                $pagination .= "<a href=\"" . $targetpage . $pagestring . "1\">1</a>";
                $pagination .= "<a href=\"" . $targetpage . $pagestring . "2\">2</a>";
                $pagination .= "<span class=\"elipses\">...</span>";
                for ($counter = $lastpage - (1 + ($adjacents * 3)); $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page)
                        $pagination .= "<span class=\"current\">$counter</span>";
                    else
                        $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";                 
                }
            }
        }

        //next button
        if ($page < $counter - 1) 
            $pagination .= "<a href=\"" . $targetpage . $pagestring . $next . "\">next �</a>";
        else
            $pagination .= "<span class=\"disabled\">next �</span>";
        $pagination .= "</div>\n";
    }

    return $pagination;

}
?>

Change the code to fit your needs and add some CSS.

There are a lot of questions about pagination, I hope this is helpful to users who in need of a pagination script..