Example using PHP Table Class
This page demonstrates how to use the PHP Table Class to generate an HTML table. The following source code results in the table displayed below the box.
<?php
require('includes/html_table.class.php');
$PRODUCTS = array(
'choc_chip' => array('Chocolate Chip Cookies', 1.25, 10.00),
'oatmeal' => array('Oatmeal Cookies', .99, 8.25),
'brownies' => array('Fudge Brownies', 1.35, 12.00)
);
// arguments: id, class
// can include associative array of optional additional attributes
$tbl = new HTML_Table('', 'demoTbl');
$tbl->addCaption('Dessert Favorites', 'cap', array('id'=> 'tblCap') );
$tbl->addRow();
// arguments: cell content, class, type (default is 'data' for td, pass 'header' for th)
// can include associative array of optional additional attributes
$tbl->addCell('Product', 'first', 'header');
$tbl->addCell('Single Item', '', 'header');
$tbl->addCell('1 Dozen', '', 'header');
foreach($PRODUCTS as $product) {
list($name, $unit_price, $doz_price ) = $product;
$tbl->addRow();
$tbl->addCell($name);
$tbl->addCell('$' . number_format($unit_price, 2), 'num' );
$tbl->addCell('$' . number_format($doz_price, 2), 'num' );
}
$tbl->addRow();
$tbl->addCell('All so very yummy!', 'foot', 'data', array('colspan'=>3) );
echo $tbl->display();
?>
The Resulting Table
This table is the result of the above code:
Product | Single | Dozen |
---|---|---|
Chocolate Chip Cookies | $1.25 | $10.00 |
Oatmeal Cookies | $0.99 | $8.25 |
Fudge Brownies | $1.35 | $12.00 |
All so very yummy! |
Download and Documentation
The PHP Table Class and examples are included in a download file. Documentation is also provided.