2010-05-16 90 views
7

我有以下方法可以在PHP中为我的分页链接创建并返回标记。分手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多个链接显示在底部起来..

,如果有人可以我想知道帮助我解决这个问题..我不确定大多数分页是如何工作的,只要显示数字就可以了。第30页,想法?

回答

4

您只显示当前页面加上以前和以下x(比如4)页面。

如果你在第1页:

1 2 3 4 5 

第35页:

31 32 33 34 35 36 37 38 39 

页70:

66 67 68 69 70 

您还可以添加快速l例如使用«»将墨水分配到第一页和最后一页。


例子:

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

您还可以实现Infinite History/Pagination,这是超级酷。 =)


UPDATE:更elegant version of this @ Codepad

+0

很不错的更新,干净,简单。 – Rabbott 2010-11-02 16:34:34

1

你可以这样做(第15页)

[View Previous] 12 13 14 [15] 15 17 18 [View More] 

凡[查看更多]链接获取剩下的(或者只是多了一些)页面的链接。这使得事情变得整洁,同时允许用户浏览所有页面。

实施例(后点击查看先前)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 [15] 15 17 18 [View More] 

或(仅示出了几个) [查看更多] 7 8 9 10 11 12 13 14 [15] 15 17 18 [查看更多]

当我说“取”我的意思是使用JavaScript来创建链接到其他网页W/O重新加载页面

+0

你以前见过这个吗?例子? – Rabbott 2010-05-16 16:28:49

0

你也可以看Zend_Paginator,它可以处理大量的这类事情的为您服务。