paging like stackoverflow's

前端 未结 3 1843
一整个雨季
一整个雨季 2021-02-11 05:24

i\'m a newbie in php especially on making pagination.

my question is, how to make paging like stackoverflow\'s pagination?
i mean paging like this :

1

相关标签:
3条回答
  • 2021-02-11 05:46

    This will generate the numbers as per above with current = 7, pages = 25. Replace the numbers with links to get an actual pagination index.

    $current = 7;
    $pages = 25;
    $links = array();
    
    if ($pages > 3) {
        // this specifies the range of pages we want to show in the middle
        $min = max($current - 2, 2);
        $max = min($current + 2, $pages-1);
    
        // we always show the first page
        $links[] = "1";
    
        // we're more than one space away from the beginning, so we need a separator
        if ($min > 2) {
            $links[] = "...";
        }
    
        // generate the middle numbers
        for ($i=$min; $i<$max+1; $i++) {
            $links[] = "$i";
        }
    
        // we're more than one space away from the end, so we need a separator
        if ($max < $pages-1) {
            $links[] = "...";
        }
        // we always show the last page
        $links[] = "$pages";
    } else {
        // we must special-case three or less, because the above logic won't work
        $links = array("1", "2", "3");
    }
    echo implode(" ", $links);
    

    Output:

    1 ... 5 6 7 8 9 ... 25
    
    0 讨论(0)
  • 2021-02-11 05:49

    Below is a snippet from a general pagination class1 I wrote a few years ago. I have edited it to show the relevant parts only.

    // cntAround is the number of pages to show before and after the current
    function renderNavigation($cntAround = 1) {
        $out      = '';
        $isGap    = false; // A "gap" is the pages to skip
        $current  = // Current page
        $cntPages = // Total number of pages
    
        for ($i = 0; $i < $pages; $i++) { // Run through pages
            $isGap = false;
    
            // Are we at a gap?
            if ($cntAround >= 0 && $i > 0 && $i < $cntPages - 1 && abs($i - $current) > $cntAround) { // If beyond "cntAround" and not first or last.
                $isGap    = true;
    
                // Skip to next linked item (or last if we've already run past the current page)
                $i = ($i < $current ? $current - $cntAround : $cntPages - 1) - 1;
            }
    
            $lnk = ($isGap ? '...' : ($i + 1)); // If gap, write ellipsis, else page number
            if ($i != $current && !$isGap) { // Do not link gaps and current
                $lnk = '<a href="?page=' . ($i + 1) . '">' . $lnk . '</a>';
            }
            $out .= "\t<li>" . $lnk . "</li>\n"; // Wrap in list items
        }
    
        return "<ul>\n" . $out . '</ul>'; // Wrap in list
    }
    

    Example 1

    cntAround = 1, current = 5, cntPages = 9:

    [1] ... [4] 5 [6] ... [9]
    

    Example 2

    cntAround = 3, current = 5, cntPages = 11:

    [1] [2] [3] [4] 5 [6] [7] [8] ... [11]
    

    1) Article is in Danish. Google Translate'd version is here.

    0 讨论(0)
  • 2021-02-11 05:55

    Somewhat like this(pseudo-code):

    pg = CurrentPageNo
    low = 1
    high = MAX_PAGES
    if (pg-low <=5)
        output 1 to pg-1 [with links]
    else
        output 1..3 [with links]
        output "..."
        output (pg-3) to (pg-1) [with links]
    
    output pg
    
    if (high - pg <=5)
        output pg+1 to high  [with links]
    else
        output (pg+1) to high-3 [with links]
        output "..."
        output (high-2) to high [with links]
    
    0 讨论(0)
提交回复
热议问题