像 stackoverflow 那样的分页

发布于 2024-11-16 00:34:13 字数 461 浏览 2 评论 0原文

我是 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 等

有没有人有简单的逻辑例子来解决这个问题?
非常感谢:)

i'm a newbie in php especially on making pagination.

my question is, how to make paging like stackoverflow's pagination?

i mean paging like this :

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

(the first number and the last number is always appear, but in the middle only 5 numbers with the selected page absolutely in the middle)

in php i have tried making paging,

<?php

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

?>

but it will be shown all of pages like

1 2 3 4 5 6 7 8 9 10 etc

any body have simple logic example to solve this problem?

many thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

赢得她心 2024-11-23 00:34:13

这将生成如上所述的数字,当前 = 7,页数 = 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

This will generate the numbers as per above with current = 7, pages = 25. Replace the numbers with links to get an actual pagination index.

$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);

Output:

1 ... 5 6 7 8 9 ... 25
败给现实 2024-11-23 00:34:13

下面是我几年前写的通用分页类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 翻译版本 在这里

Below is a snippet from a general pagination class1 I wrote a few years ago. I have edited it to show the relevant parts only.

// 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
}

Example 1

cntAround = 1, current = 5, cntPages = 9:

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

Example 2

cntAround = 3, current = 5, cntPages = 11:

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

1) Article is in Danish. Google Translate'd version is here.

月下客 2024-11-23 00:34:13

有点像这样(伪代码):

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]

Somewhat like this(pseudo-code):

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]
浅忆流年 2024-11-23 00:34:13

您可以使用 Zend_Paginator 来做到这一点,并学习使用当您使用 Zend 框架时。

You could use Zend_Paginator to do just that, and learn to use the Zend Framework while you're at it.

吹泡泡o 2024-11-23 00:34:13

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

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

Below is php classes link from where you can download php class for pagination.

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

仅此而已 2024-11-23 00:34:13

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

如何对很多很多页面进行页面导航?对数页面导航

(请注意,当然,它也适用于少量页面!)

If you (might potentially) have a large number of pages, consider using "logarithmic" page navigation, as described here (sample code included):

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

(Note that it'll work just fine for small numbers of pages, too, of course!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文