PREVIOUS 1 2 3 4 5 NEXT 5仅限页码

时间:2015-07-13 06:02:37

标签: php mysql pagination

请帮助我如何仅显示5页码..

<?php
/* Setup page vars for display. */
if ($page == 0) $page = 1;                  //if no page var is given, default to 1.
$prev = $page - 1;                          //previous page is page - 1
$next = $page + 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\">";
    //previous buttons
        if ($page > 1) 
            $pagination.= "<a class='buttons' href=\"$targetpage?page=$prev\">previous</a>";
        else
            $pagination.= "<a class='disabled'><buttons disabled>previous</buttons></a>";   

    //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 class='current'><buttons style='background-color:#CEF6F5'>$counter</buttons></a>";
            else
                $pagination.= "<a class='buttons' href=\"$targetpage?page=$counter\">$counter</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.= "<a class='current'><buttons style='background-color:#CEF6F5'>$counter</buttons></a>";
                else
                    $pagination.= "<a class='buttons' href=\"$targetpage?page=$counter\">$counter</a>";                 
            }
            $pagination.= "...";
            $pagination.= "<a class='buttons' href=\"$targetpage?page=$lpm1\">$lpm1</a>";
            $pagination.= "<a class='buttons' href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
        }
        //in middle; hide some front and some back
        elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
        {
            $pagination.= "<a class='buttons' href=\"$targetpage?page=1\"> 1 </a>";
            $pagination.= "<a class='buttons' href=\"$targetpage?page=2\"> 2 </a>";
            $pagination.= "...";
            for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<a class='current'><buttons style='background-color:#CEF6F5'>$counter</buttons></a>";
                else
                    $pagination.= "<a class='buttons' href=\"$targetpage?page=$counter\">$counter</a>";                 
            }
            $pagination.= "...";
            $pagination.= "<a class='buttons' href=\"$targetpage?page=$lpm1\">$lpm1</a>";
            $pagination.= "<a class='buttons' href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
        }
        //close to end; only hide early pages
        else
        {
            $pagination.= "<a class='buttons' href=\"$targetpage?page=1\">1</a>";
            $pagination.= "<a class='buttons' href=\"$targetpage?page=2\">2</a>";
            $pagination.= "...";
            for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<a class='current'><buttons style='background-color:#CEF6F5'>$counter</buttons></a>";
                else
                    $pagination.= "<a class='buttons' href=\"$targetpage?page=$counter\">$counter</a>";                 
            }
        }
    }

    //next buttons
    if ($page < $counter - 1) 
        $pagination.= "<a class='buttons' href=\"$targetpage?page=$next\">next</a>";
    else
        $pagination.= "<a class='buttons'>next</a>";
    $pagination.= "</div>\n";       
}
?>

SET:

$adjacents = 3
$total_pages= 22
$limit= 8
Total Records from Database: 171

预期输出:

PREVIOUS 1 2 3 4 ... 22 NEXT
PREVIOUS 1 .. 5 6 7 8 ... 22
PREVIOUS 1 .. 19 20 21 22 NEXT

但是,我无法获得预期的输出.. 我可以花一些时间来分析如何控制它产生的页码。

当前输出

   PREVIOUS 1 2 3 4 5 6 7 8 9 ... 21 22 NEXT
   PREVIOUS 1 2 ... 6 7 8 9 10 11 12 ... 21 22 NEXT
   PREVIOUS 1 2 ... 14 15 16 17 18 19 20 21 22 NEXT

2 个答案:

答案 0 :(得分:1)

        $targetpage = "example.php";    //your file name  (the name of this file)
        $limit = 15;        //how many items to show per page
        $page = @$_GET['page'];
        if($page) 
            $start = ($page - 1) * $limit; //first item to display on this page
        else
            $start = 0; //if no page var is given, set start to 0
        $tbl_name="tablename";      //your table name
        // How many adjacent pages should be shown on each side?
        $adjacents = 3;
        $query = "SELECT COUNT(*) as num FROM $tbl_name ";
        $total_pages = mysql_fetch_array(mysql_query($query));
        $total_pages = $total_pages['num'];
        /* Get data. */
        $sql = "SELECT * FROM $tbl_name LIMIT $start,$limit ";
        $result = mysql_query($sql);            
        /* Setup page vars for display. */
        if ($page == 0) $page = 1;                  //if no page var is given, default to 1.
        $prev = $page - 1;                          //previous page is page - 1
        $next = $page + 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\">";
            //previous button
            if ($page > 1) 
                $pagination.= "<a href=\"$targetpage?page=$prev\" class=\"alink_pre\"><img src=\"images/previous_but.png\" width=\"70\" height=\"24\"></a>";
            else
                $pagination.= "<span class=\"alink_pre\"><img src=\"images/previous_but.png\" width=\"70\" height=\"24\"></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=\"alink_center\" style=\"padding:0 5px;color:red;font-size:14px;\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$targetpage?page=$counter\" class=\"alink_center\" style=\"padding:0 5px\">$counter</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=\"alink_center\" style=\"padding:0 5px\">$counter</span>";
                        else
                            $pagination.= "<a href=\"$targetpage?page=$counter\" class=\"alink_center\" style=\"padding:0 5px\">$counter</a>";                  
                    }
                    $pagination.= "...&nbsp;&nbsp;&nbsp;";
                    $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>&nbsp;&nbsp;&nbsp;";

                    $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\">&nbsp;1&nbsp;</a>";
                    $pagination.= "<a href=\"$targetpage?page=2\">2&nbsp;</a>";
                    $pagination.= "...";
                    for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
                    {
                        if ($counter == $page)
                            $pagination.= "<span class=\"alink_center\" style=\"padding:0 5px\">&nbsp;$counter&nbsp;</span>";
                        else
                            $pagination.= "<a href=\"$targetpage?page=$counter\" class=\"alink_center\" style=\"padding:0 5px\">&nbsp;$counter&nbsp;</a>";                  
                    }
                    $pagination.= "...";
                    $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>&nbsp;&nbsp;&nbsp;";
                    $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
                }
                //close to end; only hide early pages
                else
                {
                    $pagination.= "<a href=\"$targetpage?page=1\">1</a>&nbsp;&nbsp;&nbsp;";
                    $pagination.= "<a href=\"$targetpage?page=2\">2</a>&nbsp;&nbsp;";
                    $pagination.= "...";
                    for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
                    {
                        if ($counter == $page)
                            $pagination.= "<span class=\"alink_center\" style=\"padding:0 5px\">&nbsp;$counter&nbsp;</span>";
                        else
                            $pagination.= "<a href=\"$targetpage?page=$counter\" style=\"padding:0 5px\" class=\"alink_center\" style=\"padding:0 5px\">&nbsp;$counter&nbsp;</a>";
                    }
                }
            }               
            //next button
            if ($page < $counter - 1) 
                $pagination.= "<a href=\"$targetpage?page=$next\" class=\"alink_next\"><img src=\"images/next_but.png\" width=\"70\" height=\"24\"></a>";
            else
                $pagination.= "<span class=\"disabled\" style=\"padding-left:40px\" class=\"alink_next\"><img src=\"images/next_but.png\" width=\"70\" height=\"24\"></span>";
            $pagination.= "</div>\n";       
        }

使用此代码并通过DB记录获取分页编号..使用mysql

答案 1 :(得分:0)

        $targetpage = "example.php";    //your file name  (the name of this file)
        $limit = 15;        //how many items to show per page
        $page = @$_GET['page'];
        if($page) 
            $start = ($page - 1) * $limit; //first item to display on this page
        else
            $start = 0; //if no page var is given, set start to 0
        $tbl_name="tablename";      //your table name
        // How many adjacent pages should be shown on each side?
        $adjacents = 3;
        $query = "SELECT COUNT(*) as num FROM $tbl_name ";
        $total_pages = mysql_fetch_array(mysql_query($query));
        $total_pages = $total_pages['num'];
        /* Get data. */
        $sql = "SELECT * FROM $tbl_name LIMIT $start,$limit ";
        $result = mysql_query($sql);            
        /* Setup page vars for display. */
        if ($page == 0) $page = 1;                  //if no page var is given, default to 1.
        $prev = $page - 1;                          //previous page is page - 1
        $next = $page + 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.
        */

使用此代码并通过DB记录获取分页编号..使用mysql