Break up PHP Pagination links

前端 未结 4 1524
清酒与你
清酒与你 2020-12-19 06:49

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

public function getPaginationLinks($options) {
    if($options[\'         


        
相关标签:
4条回答
  • 2020-12-19 07:03

    You may also look at Zend_Paginator, which handles lots of these types of things for you.

    0 讨论(0)
  • 2020-12-19 07:14

    Consider "logarithmic pagination", as described here (PHP code included):

    How to do page navigation for many, many pages? Logarithmic page navigation

    0 讨论(0)
  • 2020-12-19 07:21

    You could do (on page 15)

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

    Where the [View More] link fetches the rest (or just a few more) page links. This keeps things uncluttered while allowing the user to navigate all the pages.

    Example (after clicking View Previous)

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

    or (just show a few more) [View More] 7 8 9 10 11 12 13 14 [15] 15 17 18 [View More]

    When I say "fetch" I mean use javascript to create links to the other pages w/o reloading the page

    0 讨论(0)
  • 2020-12-19 07:25

    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.

    0 讨论(0)
提交回复
热议问题