分解 PHP 分页链接
我有以下方法可以在 PHP 中为我的分页链接创建和返回标记.
public function getPaginationLinks($options) {if($options['total_pages'] > 1) {$markup = '';if($options['page'] > 1) {$markup .= '<a href="?page=' . ($options['page'] - 1) . ((isset($options['order_by'])) ? "&sort=" . $选项['order_by'] : "") . '"><上一个';}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>';}别的 {$markup .=''.$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>';返回 $markup;}别的 {返回假;}}
我最近才发现(令我惊讶)我已经达到了 70 多个页面,这意味着现在有 70 多个链接显示在底部..
我想知道是否有人可以帮我解决这个问题.如果我说,我不确定大多数分页是如何显示数字的.第 30 页,想法?
解决方案你只需要显示当前页面加上上一页和后面的 x
(比如 4)页.
如果您在第 1 页:
1 2 3 4 5
第 35 页:
31 32 33 34 35 36 37 38 39
第 70 页:
66 67 68 69 70
例如,您还可以使用 «
和 »
添加指向第一页和最后一页的快速链接.
示例:
$x = 4;for ($i = $currentPage - $x; $i <$currentPage; $i++){if ($i >= 1) {/* 显示链接 */}else {/* 显示省略号并修复计数器 */$i = 1;}}/* 显示没有链接的当前页码 */for ($i = $currentPage + 1; $i <$currentPage + $x; $i++){if ($i <= $totalPages) {/* 显示链接 */}else {/* 显示省略号和中断 */break;}}
您还可以实现无限历史/分页,这是超级凉爽的.=)
<小时>更新:更多此@ Codepad 的优雅版本.
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;
}
}
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..
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?
解决方案You just display the current page plus the previous and the following x
(say 4) pages.
If you're on Page 1:
1 2 3 4 5
Page 35:
31 32 33 34 35 36 37 38 39
Page 70:
66 67 68 69 70
You could also add a quick link to the first and last page using «
and »
for instance.
Example:
$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. =)
UPDATE: A more elegant version of this @ Codepad.
相关文章