簡體   English   中英

如何限制分頁腳本中顯示的頁面

[英]How to limit pages shown in pagination script

我在下面發布了一個分頁腳本,問題是我有很多數據,所以我以大量的頁面結尾,我想使其一次只顯示10頁,然后像這樣最后2頁:

上一個1 2 3 4 5 6 7 8 9 ... 24 25下一個

無論如何,我可以更改代碼來執行此操作。 下面是包含的分頁腳本,如果需要,我可以包括腳本的其他部分。

<?php
//source unknown for logic of showPageNumbers()
//modified by drale.com - 1-19-2010
//added query_string reproduction and divs
//added showNext() and showPrev()

class Pagination 
{
    function getStartRow($page,$limit)
    {
        $startrow = $page * $limit - ($limit);
        return $startrow;
    }

    function showPageNumbers($totalrows,$page,$limit)
    {
        $query_string = $this->queryString();
        $pagination_links = null;

        /*
         * PAGINATION SCRIPT
         * seperates the list into pages
         */     
        $numofpages = $totalrows / $limit; 
        /* We divide our total amount of rows (for example 102) by the limit (25). This 
           will yield 4.08, which we can round down to 4. In the next few lines, we'll
           create 4 pages, and then check to see if we have extra rows remaining for 
           a 5th page. */

        for ($i = 1; $i <= $numofpages; $i++) {
            /* This for loop will add 1 to $i at the end of each pass until $i 
               is greater than $numofpages (4.08). */       
            if ($i == $page) {
                $pagination_links .= '<div class="page-link"><span>' . $i 
                                   . '</span></div> ';
            } else { 
                $pagination_links .= '<div class="page-link"><a href="?page=' . $i 
                    . '&' . $query_string . '">' . $i . '</a></div> '; 
            }

            /* This if statement will not make the current page number available 
               in link form. It will, however, make all other pages available 
               in link form. */
        }   // This ends the for loop

        if (($totalrows % $limit) != 0) {
        /* The above statement is the key to knowing if there are remainders, and it's 
        all because of the %. In PHP, C++, and other languages, the % is known as a 
        Modulus. It returns the remainder after dividing two numbers. If there is no 
        remainder, it returns zero. In our example, it will return 0.8 */

            if ($i == $page) {
                $pagination_links .= '<div class="page-link"><span>' . $i 
                                   . '</span></div> ';
            } else {
                $pagination_links .= '<div class="page-link"><a href="?page=' . $i 
                    . '&'.$query_string.'">'.$i.'</a></div> ';
            }
            /* This is the exact statement that turns pages into link 
               form that is used above */ 
        } // Ends the if statement 

        return $pagination_links;
    }

    //added by drale.com - 1-19-2010
    function showNext($totalrows,$page,$limit,$text="next &raquo;")
    {
        $next_link = null;
        $numofpages = $totalrows / $limit;

        if ($page < $numofpages) {
            $page++;
            $next_link = '<div class="page-link"><a href="?page=' . $page 
                       . '&'.$query_string.'">' . $text . '</a></div>';
        }

        return $next_link;
    }

    function showPrev($totalrows,$page,$limit,$text="&laquo; prev")
    {
        $next_link = null;
        $numofpages = $totalrows / $limit;

        if ($page > 1) {
            $page--;
            $prev_link = '<div class="page-link"><a href="?page=' . $page 
                . '&' . $query_string . '">'.$text.'</a></div>';
        }

        return $prev_link;
    }

    function queryString()
    {
        //matches up to 10 digits in page number
        $query_string = eregi_replace("page=[0-9]{0,10}&","",$_SERVER['QUERY_STRING']);
        return $query_string;
    }
} 
?>

未經測試,但這應該始終顯示列表的第1-3頁和最后3頁。 否則,它將僅顯示當前所在頁面的前3頁和后3頁。 (只要頁面數量大於10)

$alwaysShowPages = array(1, 2, 3);

// dynamically add last 3 pages
for ($i = 3; $i >= 0; $i--) {
    $alwaysShowPages[] = $numofpages - $i;
}

for ($i = 1; $i <= $numofpages; $i++) {
    $showPageLink = true;

    if ($numofpages > 10 && !in_array($i, $alwaysShowPages)) {
        if (($i < $page && ($page - $i) > 3)
            || ($i > $page && ($i - $page) > 3)
        ) {
            $showPageLink = false;
        }
    }

    if ($showPageLink) {
        if ($i == $page) {
            $pagination_links .= '<div class="page-link"><span>'.$i.'</span></div> ';
        } else { 
            $pagination_links .= '<div class="page-link"><a href="?page='.$i.'&'.$query_string.'">'.$i.'</a></div> '; 
        }
    }
}

我有一個執行此操作的版本:

1 | 2 | 3 | 4 | 5 | 6 ... 554 | 555 | 556

1 | 2 | 3 | 4 | 5 | 6 ... 554 | 555 | 556

1 | 2 | 3 ... 278 | 279 | 280 ... 554 | 555 | 556

1 | 2 | 3 ... 415 | 416 | 417 ... 554 | 555 | 556

1 | 2 | 3 | 553 | 554 | 555 | 556

...實際上是位於當前頁面和下一個(或最后一個)鏈接組的第一個之間的鏈接。

我做到了,所以如果當前頁面少於5頁,則總是出現第1 6頁。

但是我使所有參數都是動態的,因此您可以更改變量,例如:$ end_links = 3; //將兩端的鏈接數設置為始終顯示$ center_links; //包含當前頁面以在中心顯示“浮動”的鏈接數

我花了幾個小時才能做到,這真是令人傷心。 那好吧。

只需大量使用數組,然后弄清楚如何向其中添加正確的值。 確實,這是簡單的數學和邏輯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM