如何更改当前分页以显示“后退”和“下一页”而不是页码?

时间:2011-03-28 03:10:51

标签: php pagination

我希望只有当用户在任何其他页面上时才能显示“返回”,但页面#1然后“下一步”应显示在除最后一页之外的所有页面上。

 /* Setup page vars for display. */
if ($page == 0) $page = 1;  //if no page var is given,   default to 1.

  //next page is page + 1
  $lastpage = ceil($total_pages/$limit); //lastpage is = total pages 
  / 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\"></div>";
    //previous button
    if ($page > 1) 
        $pagination.= "";
    else
        $pagination.= "";   

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

               $pagination.= "...";

              for ($counter = $lastpage - (2 + ($adjacents * 2)); 
              $counter <= $lastpage; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class=\"current\">$counter</span>";
                else
                    $pagination.= 
               "<a href=\"$targetpage?page=$counter\">$counter</a>";                    
            }
        }
    }

    //next button
    if ($page < $counter - 1) 
        $pagination.= "";
    else
        $pagination.= "";       
}

&GT;

1 个答案:

答案 0 :(得分:0)

首先,声明类似$next的变量,然后分配总页数。价值如$next = $total_pages

然后检查

if($next == $total_pages + 1)
{
    do nothing....
}
else
{
    echo " <a href=\".......\">Next</a>";
}