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


How to limit pages shown in pagination script

我有一个分页脚本,我已经张贴在下面,问题是我有很多数据,所以我以一个巨大的页面列表结束,我想让它一次只显示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;
    }
} 
?>

未经测试,但这应该总是显示列表的第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,那么前6页总是显示。

但是我让所有的参数都是动态的,所以你可以这样改变变量:$end_links = 3;//设置每次显示的链接数center_links美元;//包含当前页面的链接数,在中心显示"浮动"

我花了几个小时来做这件事,这很可悲。哦。

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