Here you see the finished class with a few comments added for ease of understanding:.
<?php
/**
* class Table
* The Table class to wrap all tables
* @author NML, with thanx to Matt Zandstra
* @version 0.93 2009-11-10
*
*/
class Table {
private $tableArray = array();
private $columnHeaders = array();
/**
* Table
* @params list of column headings
*/
public function __construct() { // expects a list of column headings as args
// although no visible params
// func_get_args() takes on all comers ;-)
$this->columnHeaders = func_get_args();
}
/**
* addRow
* @params row, to be added as table data
*/
public function addRow($row) {
if (count($row) != count($this->columnHeaders)) {
echo "worng column count";
exit;
}
array_push($this->tableArray, $row);
return true;
}
/**
* displayTable
* @params name for caption
* @params id id for use in stylesheet
*
* in css style med #id {...}
* #id caption {...}
* #id th,td { ... }
*/
public function displayTable($caption, $id) {
printf("<table id='%s'>\n", $id);
printf(" <caption>%s</caption>\n", $caption);
print(" <thead><tr>\n");
foreach ($this->columnHeaders as $header)
printf(" <th>%s</th>\n", $header);
print(" </tr></thead>\n");
print(" <tbody>\n");
foreach ($this->tableArray as $row) { // for each row
print(" <tr>\n");
foreach ($row as $cell) // display it's cells
printf(" <td>%s</td>\n", $cell);
print(" </tr>\n");
}
print(" </tbody>\n");
print("</table>\n");
}
}
?>
The code allows for styling so that presentation may be according to
the user. The XHTML table elements thead and tbody
are included to add to the possibilities of styling. They allow styled scrolling of
the table head and/or the table body.
First a somewhat limited usage example. The input data are represented in the shape of a couple of arrays. Data would of course normally be read from a file or a database. But first the simple way.
<?php
ini_set("include_path", ".:../globaLib:");
require_once "myTop.inc.php";
require_once "myTable.inc.php"; // the ultimate table
// pretend three rows of data
$demoArray1 = array('Niels', 25, 11, 45);
$demoArray2 = array('Peter', 17, 03, 49);
$demoArray3 = array('Nikoline', 07, 02, 90);
?>
<!-- embedded stylesheet, quick and dirty, sincere apologies -->
<style type="text/css">
#tabel42, #tabel42 th, td { border: 1px solid blue; }
#tabel42 th { background-color: #cccccc; }
#tabel42 caption { font-weight: 700; caption-side: bottom; text-align: left; }
</style>
<?php
require_once "myNeck.inc.php";
?>
<!-- content goes here -->
<h1>Heureka! The Final Table</h1>
<p>
Here we go!
</p>
<?php
$myTable = new Table('First Name', 'Day', 'Month', 'Year'); // create an object of Table
$myTable->addRow($demoArray1); // place
$myTable->addRow($demoArray2); // three
$myTable->addRow($demoArray3); // rows
$myTable->displayTable('Yeehaaa, see my table','tabel42');// caption, the id is for css
require_once "myBottom.inc.php";
?>
Notice the include of the table class. This is essentially the way to bring it onto the stage for use.
$myTable->displayTable('Wouuvv se min tabel',"tabel42");
The string 'Wouuvv se min tabel' becomes the caption of the
table, and it may styled separately.