PHP Classes

File: example3.php

Recommend this page to a friend!
  Classes of ted kappes   Paginator   example3.php   Download  
File: example3.php
Role: Example script
Content type: text/plain
Description: Working example of use.
Class: Paginator
Spliting database query result sets between pages.
Author: By
Last change:
Date: 19 years ago
Size: 4,686 bytes


Class file image Download
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<title>Example 3</title>
<style type="text/css">
body { margin-top: 20px;
             margin-left: 150px;
             margin-right: 150px;
             background-color: #FFFF99;
             font-family: Verdana;
h1 { font-size: 150%;
<h1>Example 3</h1>
    //This shows how to set things up to get your input from an array. This could be
    //used to make an image gallery or to display anything else you might store in
    //an array. This is an example of how you can use Paginator alone to have the most
    //flexibility in setting up your links.
  //include the main class
//Makes the array used in this example.
for($i=0; $i < 25; $i++)
$pictures[$i]="pict" . $p . ".jpg";
//gets the total number of items
$num_rows = count($pictures);
        //Parts used to make a new paginator
        //Makes new Paginator. Current page here is sent by the get method.
        //$num_rows is the total items in the source.
$a =& new Paginator($_GET['page'],$num_rows);
//sets the number of records displayed
        //defaults to five
//if using numbered links this will set the number before and behind
        //the current page. defaults to five.
        //not used in this example.
        //gets starting point.
$limit1 = $a->getRange1();
//gets number of items displayed on page.
$limit2 = $a->getRange2();
        //Printing out the items in the array
for($j=$limit1; $j < $limit1 + $limit2; $j++)
"<strong>" . $pictures[$j] . "</strong></p>";
    //This shows how to create your links using the output availiable from the
    //Paginator class. It is a good example to study since it shows most of
    //the information that Paginator can output.
             //check to see if current page is one. If so there will be no link.
$first = "First | ";
                 } else {
$first="<a href=\"" . $a->getPageName() . "?page=" . $a->getFirst() . "\">First</a> |"; }
//check to see that getPrevious() is returning a value. If not there will be no link.
$prev = "<a href=\"" . $a->getPageName() . "?page=" . $a->getPrevious() . "\">Prev</a> | ";
                 } else {
$prev="Prev | "; }
//check to see that getNext() is returning a value. If not there will be no link.
$next = "<a href=\"" . $a->getPageName() . "?page=" . $a->getNext() . "\">Next</a> | ";
                 } else {
$next="Next | "; }
//check to see that getLast() is returning a value. If not there will be no link.
$last = "<a href=\"" . $a->getPageName() . "?page=" . $a->getLast() . "\">Last</a> | ";
                 } else {
$last="Last | "; }
//since these will always exist just print out the values. Result will be
                         //something like 1 of 4 of 25
echo $a->getFirstOf() . " of " .$a->getSecondOf() . " of " . $a->getTotalItems() . " ";
//print the values determined by the if statements above.
echo $first . " " . $prev . " " . $next . " " . $last;
                    //end of example
<p>This example uses the Paginator class. I use an array here to show that you
can use this class with sources other then a database. Also I did this so you could
get an idea of how this works without having to go to the trouble of setting up a
When you use Paginator alone you will need to use the items returned from methods to
make your own links. This gives you the most flexibility. Look at the source code for
this page to see how to do this. This source is a good example because it uses most
of the methods of the class.</p>
<a href="index.php">Back to Index</a>