<?php
/////////////////////////////////////////////////////////////////// // Example where `pager` is used to page and display .jpg images in // the "images" directory. The power of this class lies in it's // ability to be passed to the native PHP function `foreach` as // if it were an array. The `use_collection()` method allows you // to select which results are iterated over, possible values // being the class constants "pager::RESULTS" and "pager::PAGES". ///////////////////////////////////////////////////////////////////
try {
//calls and instantiates require_once 'pager.php'; $pager = new pager();
isset($_GET['page']) or $_GET['page'] = 1; //extracts page number from query string $images = glob('images/*.jpg'); //extracts file names into an array
//sets paging parameters $pager->size(count($images)); //total size of collection $pager->page_size(10); //display 10 results per page $pager->page($_GET['page']); //sets page number, throws exception if not number
//performs paging operations, $page_info = $pager->page_results();
////////////////////////////////////////////////////////////////////// // array keys of array returned by `page_results()` method // ------------------------------------------------------------------- // offset ............. zero-based offset of first item in the page // results ............ number of results in the page // pages .............. total number of pages // previous ........... page number of previous page, false if none // next ............... page number of next page, false if none // result range ....... displays "#first - #last" i.e. "16 - 20" // page range ......... displays "#page of #total" i.e. "2 of 4" // mysql .............. mysql "LIMIT" clause i.e. "LIMIT 2, 10" //////////////////////////////////////////////////////////////////////
//iterates over results, displays images foreach ($pager as $offset) { list($width, $height) = getimagesize($images[$offset]); //gets file' size echo "<img src={$images["$offset"]} width=\"$width\" height=\"$height\" /><br />\n"; }
$pager->use_collection(pager::PAGES); //changes collection to "PAGES" //sets value returned by `foreach`, "%d" is replaced by page number $pager->return_value(' | <a href="example.php?page=%d">%d</a>');
//iterates over pages, returns page links echo 'Go to page '; foreach ($pager as $link) { echo $link; }
//////////////////////////////////////////// // Using `pager` with `count` ////////////////////////////////////////////
//counts the number of items in the page $pager->use_collection(pager::RESULTS); $num_results = count($pager);
//counts the total number of pages $pager->use_collection(pager::PAGES); $num_pages = count($pager);
echo "<p>\n". "Results in page: $num_results<br />\n". "Total pages: $num_pages\n". "</p>";
} catch (Exception $e) { echo 'The following error occurred paging the results: '.$e->getMessage(); }
?>
|