如何限制分页脚本中显示的页面

时间:2011-11-06 10:30:14

标签: php pagination

我有一个分页脚本,我在下面发布,问题是我有很多数据所以我以一个巨大的页面列表结束,我想让它一次只显示10页然后可能是最后2页像这样的页面:

上一页1 2 3 4 5 6 7 8 9 ... 24 25 next

无论如何我可以更改代码来执行此操作。下面是包含的分页脚本,如果需要,我可以包含脚本的其他部分。

<?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;
    }
} 
?>

2 个答案:

答案 0 :(得分:1)

未经测试,但应始终显示第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 :(得分:0)

我有一个版本可以做到这一点:

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,我就会显示前6页。

但我将所有参数设置为动态,因此您可以更改变量,例如: $ end_links = 3; //设置每端的链接数始终显示 $ center_links; //链接数量,包括在中心显示“浮动”的当前页面

花了我几个小时才做到这一点,这非常难过。哦,好吧。

只需使用数组,并弄清楚如何为其添加正确的值。这是简单的数学和逻辑,真的。