PHP Classes

File: example.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in Bookmark in
  Classes of Chris Pliakas  >  Result Pager  >  example.php  >  Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example of pager class
Class: Result Pager
Split listings in multiple pages
Author: By
Last change:
Date: 7 years ago
Size: 3,182 bytes


Class file image Download

// 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();

$_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) {
$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 &nbsp; ';
    foreach (
$pager as $link) {

    // Using `pager` with `count`

    //counts the number of items in the page
$num_results = count($pager);

//counts the total number of pages
$num_pages = count($pager);

"Results in page: $num_results<br />\n".
"Total pages: $num_pages\n".

} catch (
Exception $e) {
'The following error occurred paging the results: '.$e->getMessage();