本文介绍了分解PHP分页链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下方法可以为PHP中的分页链接创建并返回标记.

I have the following method that creates and returns markup for my pagination links in PHP.

public function getPaginationLinks($options) {
    if($options['total_pages'] > 1) {
        $markup = '<div class="pagination">';

        if($options['page'] > 1) {
            $markup .= '<a href="?page=' . ($options['page'] - 1) . ((isset($options['order_by'])) ? "&sort=" . $options['order_by'] : "") . '">< prev</a>';
        }       

        for($i = 1; $i <= $options['total_pages']; $i++) {

            if($options['page'] != $i) {
                $markup .= '<a href="?page='. $i . ((isset($options['order_by'])) ? "&sort=" . $options['order_by'] : "") . '">' . $i . '</a>';
            }
            else {
                $markup .= '<span class="current">' . $i . '</span>';
            }
        }

        if($options['page'] < $options['total_pages']) {
            $markup .= '<a href="?page=' . ($options['page'] + 1) . ((isset($options['order_by'])) ? "&sort=" . $options['order_by'] : "") . '">next ></a>';
        }

        $markup .= '</div>';

        return $markup;
    }
    else {
        return false;
    }
}

我最近发现(令我惊讶)我已经浏览了70多个页面,这意味着现在底部显示了70多个链接.

I just recently discovered (to my surprise) that i had reached 70+ pages which means that there are now 70+ links showing up at the bottom..

我想知道是否有人可以帮我解决这个问题..我不确定大多数分页的效果如何,如果我要说的话显示数字..第30页,想法?

I'm wondering if someone can help me break this up.. I'm not sure how most pagination works as far as showing the numbers if im on say.. page 30, ideas?

推荐答案

您只需显示当前页面以及前一页和后一页x(例如4个)页面.

You just display the current page plus the previous and the following x (say 4) pages.

如果您位于第1页:

1 2 3 4 5

第35页:

31 32 33 34 35 36 37 38 39

第70页:

66 67 68 69 70

您还可以使用例如«»将指向第一页和最后一页的快速链接添加.

You could also add a quick link to the first and last page using « and » for instance.

示例:

$x = 4;

for ($i = $currentPage - $x; $i < $currentPage; $i++)
{
    if ($i >= 1) { /* show link */}
    else { /* show ellipsis and fix counter */ $i = 1; }
}

/* show current page number without link */

for ($i = $currentPage + 1; $i < $currentPage + $x; $i++)
{
    if ($i <= $totalPages) { /* show link */}
    else { /* show ellipsis and break */ break; }
}

您还可以实现无限历史/分页,凉爽的. =)

You can also implement Infinite History / Pagination, which is uber cool. =)

更新:更多此@ Codepad的优雅版本.

UPDATE: A more elegant version of this @ Codepad.

这篇关于分解PHP分页链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 21:22