PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Denis St-Michel  >  Simple Paginator  >  example.php  >  Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example use of this class
Class: Simple Paginator
Generate links to navigate listings split in pages
Author: By
Last change:
Date: 11 years ago
Size: 3,305 bytes


Class file image Download

// Simple Pagination class example

   // Normally, you have a php page that do some query to a database of some sort.
     // You would use this class to provide your users with a pagination list of links
     // to browse through the different pages of results

require ('class.pagination.php');

// Here you would normally have a request to the database that would count
     // the total number of record for the desired request
     // For example, we might want to get all the records from a school's students database
$query = "SELECT count(firstname) FROM students";
$result = mysql_query($query);
$total = mysql_result($result, 0);

// We should set a number of results per page
$results_per_page = 10;

// We should also get the current page viewed,
     // for example, provided by the $_GET['page'] parameter
$current_page = $_GET['page'];

// Then we figure what the limit for the MySQL query would be considering the current page
$offset = ($current_page*$results_per_page)-$results_per_page;
$limit = $off.','.$results_per_page;

// Now that we have the $limit set properly, we conduct a second request to the database
     // to get only the results we want, according to the current page number viewed.
     // The following will render as "SELECT firstname,lastname FROM students LIMIT 0,10" if
     // we are viewing page 1
$query = "SELECT firstname,lastname FROM students LIMIT ".$limit;
$result = mysql_query($query);

// We would normally process the returned rows here and do whatever you want
while ($row = mysql_fetch_array($result) {
"Firstname: ".$row['firstname']." Lastname: ".$row['lastname']."<br />";

// Then we instanciate the pagination object
$paginator = new Pagination();
// Then we set the number of results that we set per page,
     // this way the paginator class knows how many pages link there would be
// If necessary we set the captions in the language of our choice
     // By default, the class will use:
     // << as the link for the very first page
     // < as the link for previous page
     // > as the link for next page
     // >> as the link for last page
     // But if we want to use something different, we will use the following methods
   // $paginator->setCaption_first('Beginning');
   // $paginator->setCaption_previous('Previous');
   // $paginator->setCaption_next('Next');
   // $paginator->setCaption_last('Last');
     // You could also use valid HTML code for each caption, for example if you want to
     // use images links.

     // Finally we build the pagination links list
     // The $url contains the URL to get to this page, where the query to the Databse occurs
     // For example, it could be something like so:
     // The class will create an unordered list of links to browse trough pages by adding the
     // 'page' parameters to the URL above. So each link would look like:
$url = 'example.php';

// Finally we echo the results
echo $paginator->$pagination;