从阵列中的特定位置返回最后的值 [英] Return last values from specific position in array

查看:158
本文介绍了从阵列中的特定位置返回最后的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有页面阵列,

$pages = array(
   1,
   2,
   3,
   ...

   100,
   101
);

和有一个变量 $ CURRENT_PAGE 。所有我想要做的就是类似Digg的风格分页,这样它看起来像这样,

And there's a variable $current_page. All I'm trying to do is pagination in digg-like style, so that it would look like this,

< 4 5 6 7 .... 15 16 17 18 >

,想到的第一件事是从特定的立场,即等于 $ CURRENT_PAGE 获得最后和previous数组值。

The first thing that comes to mind is to get last and previous array values from specific position that equals to $current_page.

于是我开始一个基本的循环,但问题是页面的数量可能会非常大,所以我不认为这是做一个有效的事情。有没有这样做的另一种适当的方式? (也许通过本机阵列_ * 函数)?

So I started with a basic for loop, but the problem is that amount of pages could be very large, so I don't think that's an efficient thing to do. Is there any another proper way of doing this? (maybe via native array_* functions)?

推荐答案

下面的函数计算器建立像分页。其目标是:

The following function builds StackOverflow like pagination. The objectives are:


  • 第一次和最后的链接必须是可见的总是

  • previous和下一环节必须是可见的总是

  • 在最前4链接/后当前页面应该是可见

而下面的功能显示完整的寻呼机,我们在如何计算周围的页面主要兴趣是一个当前页面的函数 传呼机大小页计数

While the following function displays the complete pager, we are primarily interested in how to calculate the surrounding pages a and b as a function of current page, pager size and page count.

function so_like_pager($current_page, $page_count, $pager_size = 4) {
    if ($current_page <= $pager_size) {
        // the pager for first 4 pages starts from 1
        $a = 1;
        $b = min(1 + $pager_size, $page_count);
    } else {
        // the pager for remaining pages ends at current page + 2
        // and starts so that 4 links are shown
        $b = min($current_page + ($pager_size >> 1), $page_count);
        $a = $b - $pager_size;
    }
    // return array("show_from" => $a, "show_upto" => $b);
    echo '<p>';
    if ($current_page !== 1) {
        echo '<a href="' . so_like_pager_page(1) . '">' . 1 . '</a> ';
    } else {
        echo '<b>' . 1 . '</b> ';
    }
    if ($a > 1 + 1) {
        echo '<span>...</span> ';
    }
    for ($i = $a; $i <= $b; $i++) {
        if ($i !== 1 && $i !== $page_count) {
            if ($current_page !== $i) {
                echo '<a href="' . so_like_pager_page($i) . '">' . $i . '</a> ';
            } else {
                echo '<b>' . $i . '</b> ';
            }
        }
    }
    if ($b < $page_count - 1) {
        echo '<span>...</span> ';
    }
    if ($current_page !== $page_count) {
        echo '<a href="' . so_like_pager_page($page_count) . '">' . $page_count . '</a> ';
    } else {
        echo '<b>' . $page_count . '</b> ';
    }
    echo '</p>';
}
function so_like_pager_page($page) {
    return 'page-' . $page . '/';
}

测试:

so_like_pager(1, 100);
so_like_pager(2, 100);
so_like_pager(3, 100);
so_like_pager(4, 100);
so_like_pager(5, 100);
so_like_pager(6, 100);
so_like_pager(50, 100);
so_like_pager(99, 100);
so_like_pager(100, 100);

输出:

PS:注意:我从ASP经典急于PHP移植此功能并没有测试对边界情况

PS: Note: I ported this function from ASP classic to PHP in a hurry and did not test against edge cases.

这篇关于从阵列中的特定位置返回最后的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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