showing surrounding page numbers

Posted by Tony Vipros on Stack Overflow See other posts from Stack Overflow or by Tony Vipros
Published on 2010-04-21T14:36:59Z Indexed on 2010/04/21 14:53 UTC
Read the original article Hit count: 325

Filed under:
|
|

I've been doing some pagination recently and used the following:

if ( $totalPages > $pagesToShow ) {
    $start = $pageNumber - floor($pagesToShow/2);
    $end = $pageNumber + floor($pagesToShow/2);

    while ( $start < 1 ) {
        $start++;
        $end++;
    }

    while ( $end > $totalPages ) {
        $start--;
        $end--;
    }

} else {
    $start = 1;
    $end = $totalPages;
}

to work out where to start and end the list of surrounding pages. So that a paging list can be created like << < 1 2 3 4 5 > >>'.

Just wondering if there is a better method as using loops like that seems a little odd.

© Stack Overflow or respective owner

Related posts about php

Related posts about pagination