Long Pagination Test

The Problem

This is inspired by a question on the comp.lang.php newsgroup. The poster wanted pagination which would display the first few pages, then skip in larger steps until the end. This is a quick and dirty version which could stand some tidying and / or modifying... It's been one of them days! ;-)

The effect of the script is below, and then underneath that is the code that does the job.

The Effect...

Number of pages: 7, Menu: [1][2][3][4]... [7]

Number of pages: 4, Menu: [1][2][3][4]

Number of pages: 3, Menu: [1][2][3]

Number of pages: 10, Menu: [1][2][3][4]... [10]

Number of pages: 25, Menu: [1][2][3][4]... [10]... [25]

Number of pages: 33, Menu: [1][2][3][4]... [10]... [20]... [33]

Number of pages: 50, Menu: [1][2][3][4]... [10]... [20]... [30]... [40]... [50]

The Code


<?php


$start 
1;
$pagesarray = array(74310253350);

$limit 4;

foreach(
$pagesarray as $pages) {
    print(
"<p><em>Number of pages: ".$pages."</em>, Menu: ");
    for(
$index $start$index <= $limit$index ++) {
        if(
$index <= $pages) {
            print(
"[".$index."]");
        }
    }
    if(
$pages $limit) {
        if(
$pages 10) {
            print(
"... [".$pages."]");
        }
        else {
            for(
$index 10$index $pages 1$index += 10) {
                if(
$pages $index 10) {
                    print(
"... [".$pages."]");
                }
                else {
                    print(
"... [".$index."]");
                }
            }
        }
    }
    print(
"</p>\n");
}
?>

You are free to use / modify this code, although I would appreciate a link back in return (either to the article itself, or my homepage). An email to let me know would be nice too...

[email protected] | www.nigenet.org.uk