如何限制分页脚本中显示的页面 [英] How to limit pages shown in pagination script

查看:69
本文介绍了如何限制分页脚本中显示的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a pagination script which I have posted below, the problem is I have alot of data so I end with a huge list of pages, I want to make it show only 10 pages at a time and then maybe the last 2 pages like this:

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

previous 1 2 3 4 5 6 7 8 9...24 25 next

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

is there anyway I can change the code to do this. Below is the included pagination script I can include the other part of script if needed.

<?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)

Untested, but this should always show pages 1 - 3 and the last 3 pages of the list. Otherwise, it will only ever show the previous 3 pages and the next three pages from the current one you're on. (whenever the amount of pages is greater than 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> '; 
        }
    }
}

这篇关于如何限制分页脚本中显示的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆