Location: PHPKode > scripts > Table wrapper base class > table-wrapper-base-class/test_table_class.php
<?php
/*
 * test_table_class.php
 *
 * @(#) $Id: test_table_class.php,v 1.1 2008/06/08 22:27:43 mlemos Exp $
 *
 */

/*
 *  Require the base table class
 */
	require('tableclass.php');

/*
 *  A sub-class of the table class to customize how to retrieve the data
 *  to be presented
 */

class my_table_class extends table_class
{
	/*
	 *  Initial first page
	 */
	var $page = 0;

	/*
	 *  Limit of rows to show per page besides the header row
	 */
	var $rowsperpage = 10;

	/*
	 *  Turn table border on or off
	 */
	var $border = 0;

	/*
	 *  Color to highlight rows when the users drags the mouse over them
	 */
	var $highlightrowcolor = '#00CCCC';

	/*
	 *  Background color of the header row
	 */
	var $headersrowbackgroundcolor = '#CCCCCC';

	/*
	 *  Background color of the odd numbered rows
	 */
	var $oddrowsbackgroundcolor = '#EEEE00';

	/*
	 *  Background color of the even numbered rows
	 */
	var $evenrowsbackgroundcolor = '#CCCC00';

	/*
	 *  Array of values to be displayed
	 */
	var $values = array();

	/*
	 *  Titles of the header columns
	 */
	var $titles = array();

	/*
	 *  This function defines the contents of each table cell
	 */
	Function fetchcolumn(&$columndata)
	{
		$column = $columndata['column'];
		if($column >= count($this->titles))
			return 0;

		/*
		 *  Is it the header row?
		 */
		$row = $columndata['row'];
		if($row==0)
		{
			/*
			 *  Display the table header titles
			 */
			$columndata['data'] = $this->titles[$column];
			$columndata['header']=1;
		}
		else
		{
			/*
			 *  Display the table cells with data from the values array
			 */
			$value=Key($this->values);
			switch($column)
			{
				case 0:
					$columndata['data'] = $this->values[$value]['id'];
					$columndata['align'] = 'right';
					break;

				case 1:
					$columndata['data'] = $this->values[$value]['currency'];
					break;

				case 2:
					$columndata['data'] = $this->values[$value]['zone'];
					break;

				case 3:
					$columndata['data'] = $this->values[$value]['name'];
					break;

				case 4:
					$columndata['data'] = $this->values[$value]['current_value'];
					$columndata['align'] = 'right';
					break;
			}
		}
		return(1);
	}

	/*
	 *  Function that defines each table row
	 */
	Function fetchrow(&$rowdata)
	{
		/*
		 *  Only allow displaying up to the limit number of rows
		 */
		$row = $rowdata['row'];
		if($row > min($this->rowsperpage, count($this->values) - $this->page * $this->rowsperpage))
			return(0);

		/*
		 * Set the highlight and background color according to the row number 
		 */
		$rowdata['backgroundcolor']=(($row == 0) ? $this->headersrowbackgroundcolor : ((intval($row % 2) == 0) ? $this->evenrowsbackgroundcolor : $this->oddrowsbackgroundcolor));
		$rowdata['id']=($this->rowidprefix.strval($row));
		$rowdata['highlightcolor']=(($row != 0) ? $this->highlightrowcolor : '');
		switch($row)
		{
			case 0:
				/*
				 *  Seek to the first position of the array values to display
				 */
				Reset($this->values);
				$first = $this->page * $this->rowsperpage;
				for($p = 0; $p < $first; ++$p)
					Next($this->values);
				break;
			case 1:
				break;
			default:
				/*
				 *  Seek to the next position of the array values to display
				 */
				Next($this->values);
				break;
		}
		return(1);
	}
};

?><html>
<head>
<title>Test for Manuel Lemos' PHP table class</title>
</head>
<body>
<h1><center>Test for Manuel Lemos' PHP table class</center></h1>
<hr />
<?php

	/*
	 *  Array of data to display in the table
	 */
	$currencies = array(
		array('id'=>1,  'currency'=>'XEU', 'zone'=>'Europe',         'name'=>'Euro',      'current_value'=>'1.0000000'),
		array('id'=>2,  'currency'=>'PTE', 'zone'=>'Portugal',       'name'=>'Escudo',    'current_value'=>'200.4820000'),
		array('id'=>3,  'currency'=>'DEM', 'zone'=>'Germany',        'name'=>'Mark',      'current_value'=>'1.9558300'),
		array('id'=>4,  'currency'=>'FRF', 'zone'=>'France',         'name'=>'Franc',     'current_value'=>'6.5595700'),
		array('id'=>5,  'currency'=>'ESP', 'zone'=>'Spain',          'name'=>'Peseta',    'current_value'=>'166.3860000'),
		array('id'=>6,  'currency'=>'ITL', 'zone'=>'Italy',          'name'=>'Lira',      'current_value'=>'1936.2700000'),
		array('id'=>7,  'currency'=>'IEP', 'zone'=>'Ireland',        'name'=>'Punt',      'current_value'=>'0.7875640'),
		array('id'=>8,  'currency'=>'BEP', 'zone'=>'Belgium',        'name'=>'Franc',     'current_value'=>'40.3399000'),
		array('id'=>9,  'currency'=>'LUF', 'zone'=>'Luxembourg',     'name'=>'Franc',     'current_value'=>'40.3399000'),
		array('id'=>10, 'currency'=>'ATS', 'zone'=>'Austria',        'name'=>'Schilling', 'current_value'=>'13.7603000'),
		array('id'=>11, 'currency'=>'NLG', 'zone'=>'Netherlands',    'name'=>'Guilder',   'current_value'=>'2.2037100'),
		array('id'=>12, 'currency'=>'FIM', 'zone'=>'Finland',        'name'=>'Markka',    'current_value'=>'5.9457300'),
		array('id'=>13, 'currency'=>'USD', 'zone'=>'United States',  'name'=>'Markka',    'current_value'=>'0.9265910'),
		array('id'=>14, 'currency'=>'CHF', 'zone'=>'Switzerland',    'name'=>'Franc',     'current_value'=>'1.5188700'),
		array('id'=>15, 'currency'=>'CAD', 'zone'=>'Canada',         'name'=>'Dollar',    'current_value'=>'1.4058900'),
		array('id'=>16, 'currency'=>'JPY', 'zone'=>'Japan',          'name'=>'Yen',       'current_value'=>'105.0500000'),
		array('id'=>17, 'currency'=>'BRL', 'zone'=>'Brazil',         'name'=>'Real',      'current_value'=>'1.8233200'),
		array('id'=>18, 'currency'=>'DKK', 'zone'=>'Denmark',        'name'=>'Kroner',    'current_value'=>'7.4571300'),
		array('id'=>19, 'currency'=>'GRD', 'zone'=>'Greece',         'name'=>'Drachma',   'current_value'=>'340.3260000'),
		array('id'=>20, 'currency'=>'RUR', 'zone'=>'Russia',         'name'=>'Ruble',     'current_value'=>'25.9535000'),
		array('id'=>21, 'currency'=>'SEK', 'zone'=>'Sweeden',        'name'=>'Krona',     'current_value'=>'8.8321600'),
		array('id'=>22, 'currency'=>'GBP', 'zone'=>'United Kingdom', 'name'=>'Pound',     'current_value'=>'0.6274870'),
	);

	$table = new my_table_class;

	/*
	 *  Prefix for the table row identifiers
	 */
	$table->rowidprefix = 'currency';

	/*
	 *  Titles of the table columns
	 */
	$table->titles = array(
		'ID',
		'Symbol',
		'World zone',
		'Name',
		'Current value'
	);

	/*
	 *  Limit of navigation links to show
	 */
	$table->listpages=3;

	/*
	 *  Title of the first page link
	 */
	$table->firstprefix="<< First";

	/*
	 *  Title of the previous page link
	 */
	$table->previousprefix="< Previous";

	/*
	 *  Title of the next page link
	 */
	$table->nextsuffix="Next >";

	/*
	 *  Title of the last page link
	 */
	$table->lastsuffix="Last >>";

	/*
	 *  Show the row range in the first and last page links
	 */
	$table->rangeinfirstlast=0;

	/*
	 *  Show the row range in the previous and next page links
	 */
	$table->rangeinpreviousnext=0;

	/*
	 *  Limit number of rows to display per page
	 */
	$table->rowsperpage = 5;

	/*
	 *  Set the array of values to display in the table
	 */
	$table->values = $currencies;

	/*
	 *  Set the total number of rows to display in all pages
	 *  so the class can generate pagination links
	 */
	$table->totalrows = count($currencies);

	/*
	 *  Set the number of the current page to display
	 */
	$maximum_pages = intval($table->totalrows / $table->rowsperpage);
	if(IsSet($_GET['page'])
	&& ($page = intval($_GET['page'])) >=0
	&& $page <= $maximum_pages)
		$table->page = $page;

	/*
	 *  Display the whole table at once
	 */
	echo $table->outputtable();
?>
<hr />
</body>
</html>
Return current item: Table wrapper base class