像stackoverflow一样分页

时间:2011-06-15 07:16:04

标签: php pagination logic paging

我是php的新手,特别是在制作分页时。

我的问题是,如何使分页像stackoverflow的分页?
我的意思是这样的分页:

1 ... 5 6 7 8 9 ... 25
(始终显示第一个数字和最后一个数字,但中间只有5个数字,所选页面绝对位于中间)

在php中我已经尝试过分页,

<?php

//Show page links
for($i=1; $i<=$pages; $i++)
{
    echo '<li id="'.$i.'">'.$i.'</li>';
}

?>

但会显示所有页面

1 2 3 4 5 6 7 8 9 10等

任何机构都有简单的逻辑示例来解决这个问题?
非常感谢:))

6 个答案:

答案 0 :(得分:13)

这将生成上面的数字,其中current = 7,pages = 25.用链接替换数字以获得实际的分页索引。

$current = 7;
$pages = 25;
$links = array();

if ($pages > 3) {
    // this specifies the range of pages we want to show in the middle
    $min = max($current - 2, 2);
    $max = min($current + 2, $pages-1);

    // we always show the first page
    $links[] = "1";

    // we're more than one space away from the beginning, so we need a separator
    if ($min > 2) {
        $links[] = "...";
    }

    // generate the middle numbers
    for ($i=$min; $i<$max+1; $i++) {
        $links[] = "$i";
    }

    // we're more than one space away from the end, so we need a separator
    if ($max < $pages-1) {
        $links[] = "...";
    }
    // we always show the last page
    $links[] = "$pages";
} else {
    // we must special-case three or less, because the above logic won't work
    $links = array("1", "2", "3");
}
echo implode(" ", $links);

输出:

1 ... 5 6 7 8 9 ... 25

答案 1 :(得分:10)

以下是几年前我写的general pagination class 1 的摘录。我编辑它只显示相关部分。

// cntAround is the number of pages to show before and after the current
function renderNavigation($cntAround = 1) {
    $out      = '';
    $isGap    = false; // A "gap" is the pages to skip
    $current  = // Current page
    $cntPages = // Total number of pages

    for ($i = 0; $i < $pages; $i++) { // Run through pages
        $isGap = false;

        // Are we at a gap?
        if ($cntAround >= 0 && $i > 0 && $i < $cntPages - 1 && abs($i - $current) > $cntAround) { // If beyond "cntAround" and not first or last.
            $isGap    = true;

            // Skip to next linked item (or last if we've already run past the current page)
            $i = ($i < $current ? $current - $cntAround : $cntPages - 1) - 1;
        }

        $lnk = ($isGap ? '...' : ($i + 1)); // If gap, write ellipsis, else page number
        if ($i != $current && !$isGap) { // Do not link gaps and current
            $lnk = '<a href="?page=' . ($i + 1) . '">' . $lnk . '</a>';
        }
        $out .= "\t<li>" . $lnk . "</li>\n"; // Wrap in list items
    }

    return "<ul>\n" . $out . '</ul>'; // Wrap in list
}

示例1

cntAround = 1current = 5cntPages = 9

[1] ... [4] 5 [6] ... [9]

示例2

cntAround = 3current = 5cntPages = 11

[1] [2] [3] [4] 5 [6] [7] [8] ... [11]

1)文章是丹麦语。 Google翻译版is here

答案 2 :(得分:2)

有点像这样(伪代码):

pg = CurrentPageNo
low = 1
high = MAX_PAGES
if (pg-low <=5)
    output 1 to pg-1 [with links]
else
    output 1..3 [with links]
    output "..."
    output (pg-3) to (pg-1) [with links]

output pg

if (high - pg <=5)
    output pg+1 to high  [with links]
else
    output (pg+1) to high-3 [with links]
    output "..."
    output (high-2) to high [with links]

答案 3 :(得分:1)

您可以使用Zend_Paginator来做到这一点,并学习如何使用Zend Framework。

答案 4 :(得分:1)

下面是php classes链接,您可以从这里下载php类以进行分页。

http://www.phpclasses.org/search.html?words=paging&x=0&y=0&go_search=1

答案 5 :(得分:0)

如果您(可能)有大量页面,请考虑使用“对数”页面导航,如此处所述(包含示例代码):

How to do page navigation for many, many pages? Logarithmic page navigation

(请注意,当然,它对于少量页面也可以正常工作!)