dongpan1365 2014-04-12 16:41
浏览 49
已采纳

返回数组中特定位置的最后一个值

There's an array of pages,

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

   100,
   101
);

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 >

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

  • 写回答

4条回答 默认 最新

  • douan9541 2014-04-12 17:11
    关注

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

    • First and last links must be visible always
    • Previous and next links must be visible always
    • At most 4 links before/after the current page should be visible

    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 . '/';
    }
    

    Tests:

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

    Output:

    Pagination code output

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 请求分析基于spring boot+vue的前后端分离的项目
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?