PHP分页颠倒

时间:2015-09-05 18:37:53

标签: php mysql pagination

我有标准的分页效果很好。我想要做的就是让它颠倒过来。所以导航将从最后开始。 5 | 4 | 3 | 2 | 1。我的每页限制为15,所以我希望页面nr 1包含15个元素,最后一页将包含最新元素。我知道这是非常不寻常的方法,但这就是我想做的。但我需要有人让我走上正确的道路,请帮助一下。感谢。

我的功能代码:(缩写)

<?

$adjacents = 4;

$result = $mysqli->query("SELECT * FROM table");
$total_pages = $result->num_rows;
$result->close();

    /* Setup vars for query. */
    $targetpage = "articles";
    $limit = 15;                                
    $page = $_GET['page'];
    if($page) 
        $start = ($page - 1) * $limit;  
    else
        $start = 0;         

    /* Get data. */
    $sql = $mysqli->query("SELECT * FROM table ORDER BY id DESC LIMIT $start, $limit");


    /* 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


while($row = $sql->fetch_array())  {

$name = $row['name'];
$ource = $row['source'];

}

?>

<?=$pagination?>
<? $sql->close(); ?>

3 个答案:

答案 0 :(得分:2)

嗯...第一个提示 - 不要使用短开启的php标签。它们已被弃用,可能会在某些服务器上出现问题。使用<?php启动php代码。

其次,在您的代码中,您有混合部分,它们自己显示寻呼机和页面内容。 mySql查询用于获取当前页面的行。但对于寻呼机,您将需要类似的查询,它将返回所有行的计数(因此没有限制部分)。

 $sql = $mysqli->query("SELECT COUNT(*) FROM table");

像这样......

当你得到那个计数时(让我们说你总共有45行)。您必须计算总页数。这将是:

$pages_number = ceil ($rows_count / $limit );

在我们的案例中,我们将有3个(页面)。

然后,你需要一些for循环,从页数到1,如:

for ($i = $pages_number; $i > 1; $i--){
 // write out current link
}

此循环将为您提供数字3,2,1 ...因此,请创建链接并将其打印出来。

答案 1 :(得分:0)

    public class Banners
{

    public int ID{get;set;}
    public string Name{get;set;}

    //other columns..

    public IList<Page> Pages{get;set;}

}


public class Page
{

    public int ID{get;set;}
    public string Name{get;set;}

    //other columns..

    public IList<Banners> Bannerss{get;set;}

}
//MAKE THIS TABLE MANUALLY..?
public class BannersInPages

{

    public int BannerId{get;set;}
    public int PageId{get;set;}
    public int BannerSortOrder{get;set;}
    public string Placement {get;set;}  
}

答案 2 :(得分:0)

<?php
$result = $mysqli->query("SELECT id FROM table");
$total_results = $result->num_rows;
$result->close();

    /* Setup vars for query. */

    $targetpage = "moving-articles";
    $limit = 5; 
    $adjacents = 5;
    $lastpage = ceil($total_results/$limit);                                
    $page = $_GET['page'];
    if( $page) 
        $start = ($page - 1) * $limit;  
    else
        $start = ($lastpage - 1) * $limit;; 

    /* Get data. */
    $sql = $mysqli->query("(SELECT * FROM table ORDER BY datestamp LIMIT $start, $limit) ORDER BY datestamp DESC");
    if ($page == '') $page = $lastpage; //if no page var is given, default to last page (as first page)
    $prev = $page - 1;                          //previous page is page - 1
    $next = $page + 1;                          //next page is page + 1
    $lpm1 = $lastpage - 1;                          


    /* Draw the pagination object.  */

    $pagination = "";
    if($lastpage > 1)
    {   
        $pagination .= "
        <div class='pagination'>";
        /* Next Button */
        if ($page < $lastpage) 
            $pagination.= "<a href='$targetpage/page/$next/'>« next</a>";
        else
            $pagination.= "<span class='disabled'>« next</span>";   

        if ($lastpage < $adjacents * 4) //not enough pages to bother breaking it up
        {   
        /* Backwards Pagination */
        for ($counter = $lastpage; $counter > 0; $counter--) {
                if ($counter == $page)
                    $pagination.= "<span class='current'>$counter</span>";
                else
                    $pagination.= "<a href='$targetpage/page/$counter/'>$counter</a>";
        }
        }

        else if($lastpage > $adjacents * 4) //enough pages to hide some
        {
            //close to beginning; only hide later pages
            if($page > $lastpage - $adjacents * 2)      
            {

                for ($counter = $lastpage ; $counter > abs(($adjacents * 2) - $lastpage) - 1; $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/2/'>2</a>";
                $pagination.= "<a href='$targetpage/page/1/'>1</a>";

            }
        //in middle; hide some front and some back
            else if($page <= $lastpage - $adjacents * 2 && $page > $adjacents * 2)
            {

                $pagination.= "<a href='$targetpage/page/$lastpage/'>$lastpage</a>";    
                $pagination.= "<a href='$targetpage/page/$lpm1/'>$lpm1</a>";
                $pagination.= "...";
                for ($counter = $page + $adjacents; $counter > $page - $adjacents - 1 ; $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/2/'>2</a>";
                $pagination.= "<a href='$targetpage/page/1/'>1</a>";

            }
            //close to end; only hide early pages
            else
            {
                $pagination.= "<a href='$targetpage/page/$lastpage/'>$lastpage</a>";    
                $pagination.= "<a href='$targetpage/page/$lpm1/'>$lpm1</a>";
                $pagination.= "...";
                for ($counter = ($adjacents * 2) +1 ; $counter >= 1; $counter--)
                {
                    if ($counter == $page)
                        $pagination.= "<span class='current'>$counter</span>";
                    else
                        $pagination.= "<a href='$targetpage/page/$counter/'>$counter</a>";                  
                }
            }

        }


        /* Previous Button */
        if ($page > $counter - 1) 
            $pagination.= "<a href='$targetpage/page/$prev/'>previous »</a>";
        else
            $pagination.= "<span class='disabled'>previous »</span>";

        $pagination.= "</div>";     
    }

?>