woutr_be
woutr_be

Reputation: 9722

Pagination like Flickr

I recently ran into a problem with my pagination, it happens that there are like a 100 pages, so obviously my pagination becomes way to long.

Now I want to prevent this by removing some links, so instead of

1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10

I want to show it as

1 - 2 - 3 - 4 - 5 - ... - 10

Or when you're on page 4

1 - ... - 4 - 5 - 6 - ... 10

At the moment the script that displays my pagination goes like this:

$links = array();
for($i = 0; $i < $totalPages; $i++) {
    array_push($links, $i);
    }

foreach($links as $pageLink) {
    if($pageLink == $page) {
    $pagination .= "<li><a href='" . BASE_URL . $url . $pageLink . ".html' class='active'>" . ($pageLink + 1) . "</a></li>";
    } else {
        if($pageLink === '...') {
            $pagination .= "<li><a href='#'>...</a></li>";
        } else {
            $pagination .= "<li><a href='" . BASE_URL . $url . $pageLink . ".html'>" . ($pageLink + 1) . "</a></li>";
        }
    }   
}

I added all the links in an array so it would be easy to adjust this, but I can't figure out how I can get the pagination like that.

Upvotes: 0

Views: 343

Answers (1)

Lajos Arpad
Lajos Arpad

Reputation: 76426

Somehow you can get the current page number and the last page number. The pager item which represents your current page should be somewhat different from your other pages. If your page number is smaller or equal than 3 you show 1 2 3 ... LastPageNumber and if your current page is greater or equal than LastPageNumber - 2 then your paging should look like 1 ... 67 68 69. Otherwise your paging should look like 1 ... 30 31 32 33 34 ... 69 (in this example your current page is 32).

Upvotes: 1

Related Questions