Location: PHPKode > scripts > TPaginatedData > tpaginateddata/pagination_example.php
<?
/*
	This script is an example on how to use TPaginationData class in real life.

	We consider that a MySQL database connection already exists.
*/

// code to connect to MySQL server and select the database would be here...

// a dummy query string to select id and title of products:
$query = "SELECT id, title FROM products ORDER BY title";

// we include the class file:
include_once 'TPaginatedData.class.php';

// we show 20 items in each page. if you don't specify it in constructor, the default is 10.
$itemsPerPage = 20; 

// there will be 5 navigation links (i.e. First 1 2 3 4 5 Last). if not specified in constructor, default is 10.
$visiblePageLinks = 5; 

// instanciate an object from the class:
$myPaginatedData = new TPaginatedData($query, $itemsPerPage, $visiblePageLinks);

// the page number we want to display.
// $_GET['page'] is used to hold the page number user wants to see, so we check if we have it specified 
// (if it's not specified then we should show 1st page)
if( $_GET['page'] > 0 )
	$pageToShow = $_GET['page'];
else
	$pageToShow = 1;

// we get the data for this page:
$myPaginatedData->fetchPage( $pageToShow );

// now we have the data for that page in $myPaginatedData->rs in case we want to use it somewhere. In this example we won't.. :)

// we display the page number links for navigation:
$myPaginatedData->pageLinks('index.php?', $pageToShow, false); // false is because we don't want URL rewrite type links - read the comments in the class file for explanations

// we output the actual data, using products_listing_tpl.php as a template - take a look at it.
$myPaginatedData->output("products_listing_tpl.php");

// and we display the navigation links at the bottom too:
$myPaginatedData->pageLinks('index.php?', $pageToShow, false);


// that's all.

?>
Return current item: TPaginatedData