PHP Classes

File: demo.php

Recommend this page to a friend!
  Classes of Kjetil H   Page Lister   ???   Download  
File: ???
Role: Example script
Content type: text/plain
Description: Simple demo for package usage
Class: Page Lister
Split listings of entries in multiple pages
Author: By
Last change: Added prev and next page check to improve demo and illustrate to the user how to make only valid urls clickable.
Date: 14 years ago
Size: 1,947 bytes
 

Contents

Class file image Download
<?php
require_once 'PageLister.php';

echo
'<h1>Introducing the PageLister</h1>';

echo
'<h3>Simple example using only default options.</h3>';

/**
 * Preparation only requires the current page number and
 * an array containing all the items.
 *
 * We assign some dummy items for testing.
 */

$currentPage = !empty($_GET['page']) ? (int) $_GET['page'] : 1;

$allItems = array();
for (
$i=0; $i<25; $i++) $allItems[] = $i;

/**
 * Making the pagelist is a breeze. Feed in the data and extract it.
 */

$pl = new PageLister($allItems, $currentPage);
$pageList = $pl->makePageList();
$currentItems = $pl->getCurrentItems();

/**
 * Finally output the current page items and the pagelist
 */

echo "Current page items:<br />\n";

foreach (
$currentItems as $item)
{
  echo
"$item <br />\n";
}

echo
"Pages: \n";

foreach (
$pageList as $page)
{
       
// make sure only valid urls are clickable (prev page at page 1 won't yield a valid previous next page)
   
if ($page['url']) {
      echo
"<a href='?page={$page['page']}'>{$page['label']}</a> ";
    }
    else {
      echo
$page['label'].' ';
    }
}

echo
'<br />-------------------------------------------------';
echo
'<h3>Example using page labels.</h3>';

/**
 * Example using page labels.
 *
 * First define the page labels and assign dummy articles.
 */

$pageLabels = array(
 
1 => 'Introduction',
 
2 => 'Contents',
 
3 => 'Analysis',
 
4 => 'Summary',
 
5 => 'Litterature'
);

$allItems = array();
for (
$i=0; $i<5; $i++) $allItems[] = $i;

/**
 * Instantiate the pagelister with options.
 */

$pl = new PageLister($allItems, $currentPage, array(
 
'pageLabels' => $pageLabels,
 
'itemsPerPage' => 1,
 
'prevAndNext' => false
));
$pageList = $pl->makePageList();

/**
 * Output the pages.
 */

foreach ($pageList as $page)
{
    echo
"<a href='?page={$page['page']}'>{$page['label']}</a> <br />\n";
}
?>