PHP Table Class

Posted on Updated on


Here is a PHP class that creates an HTML table
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxx FILE INFORMATION xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// @title : HTML Table Class
// @author : Jhoy Q. Imperial
// @date created : February 7, 2008
// @function : creates an HTML table object;
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
class Table
{
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxx DECLARE CLASS VARIABLES xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
var $tVars;

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxx CREATE NEW TABLE OBJECT xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
function Table($name,$attribute=NULL)
{
/*
Set table name and attribute
attribute string should be written as it is in HTML tag
ex. class=”tableclass” border=”1″
*/
$this->tVars[’name’] = ‘name=”‘.$name.’” id=”‘.$name.’” ‘.$attribute;
}

function setHead($name=NULL,$col)
{
/*
Set table header name and corresponding column title
*/
$name = (is_null($name))? ”: ‘id=”‘.$name.’”‘;
/*
store table header row in $this->tVars[’head’]
*/
$this->tVars[’head’] = “\t”.’<tr ‘.$name.’>’;
/*
run through $col if it is a valid array variable
*/
if(is_array($col)){
foreach($col as $value){
$this->tVars[’head’].=’<td>’.$value.’</td>’;
}
}
$this->tVars[’head’].=’</tr>’;
}
function setData($data)
{
/*
xxxxx This sets data in normal table rows xxxxx

Set table data values
store row of data in $this->tVars[’data’]
*/
$this->tVars[’data’]=”;
/*
run through $data if it is a valid array variable
*/
if(is_array($data)){
foreach($data as $row){
$this->tVars[’data’].=”\t”.’<tr>’;
/*
run through $data if it is a valid array variable
*/
if(is_array($row)){
foreach($row as $key=>$value){
$this->tVars[’data’].=’<td>’.$value.’</td>’;
}
}
$this->tVars[’data’].=’</tr>’.”\n”;
}
}
}
function setAlternate($data)
{
/*
xxxxx This sets data in alternating table rows xxxxx

assign variables
$rowCount = start count for foreach row
$rowOdd = id string for odd row
$rowEven = id string for even row
*/
$rowCount = 0;
$rowOdd = ‘ id=”odd” ‘;
$rowEven= ‘ id=”even” ‘;
/*
Set table data values
store row of data in $this->tVars[’data’]
*/
$this->tVars[’data’]=”;
/*
run through $data if it is a valid array variable
*/
if(is_array($data)){
foreach($data as $key=>$row){
$rowCount++;
$checkOddEven = $rowCount %2;
$displayRowID = ($checkOddEven==0)? $rowOdd:$rowEven;

$this->tVars[’data’].=”\t”.’<tr’.$displayRowID.’>’;
/*
run through $row if it is a valid array variable
*/
if(is_array($row)){
foreach($row as $key=>$value){
$this->tVars[’data’].=’<td>’.$value.’</td>’;
}
}
$this->tVars[’data’].=’</tr>’.”\n”;
}
}
}
function getTable()
{
/*
store table object to $this->tVars[’table’]

$this->tVars[’name’] = the table name and attributes
$this->tVars[’head’] = table header row
$this->tVars[’data’] = table data row(s)
*/
$this->tVars[’table’] =’<table ‘.$this->tVars[’name’].’ >’.”\n”;
$this->tVars[’table’].=$this->tVars[’head’].”\n”;
$this->tVars[’table’].=$this->tVars[’data’];
$this->tVars[’table’].=”\t”.’<table>’.”\n”;
/*
return $this->tVars[’table’] for output display
*/
return $this->tVars[’table’];
}
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxx HOW TO USE xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// A sample demo for this class
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
/*
include ‘class.table.php’;

$data = array();
$data[]= array(’2′,’Johnny’);
$data[]= array(’4′,’Kathy’);
$data[]= array(’5′,’McCain’);

// @param : new Table($string table name, $string atttribute [’attrname=”value” attrname=”value”‘]);
$table = new Table(’resultTable’, ‘class=”newTable”‘);
// @param : setHead($string row name,$array col names);
$table->setHead(’head’,array(’ID’,’Name’));
// @param : setData($array data value)
$table->setData($data);
// use setAlternate($data) if you want to display with alternating row design
// @param : setAlternate($array data value)
//$table->setAlternate($data);
echo $table->getTable();
*/
?>

2 thoughts on “PHP Table Class

    guest said:
    May 29, 2008 at 6:52 PM

    Nice one and a very clear one.

    marian said:
    May 30, 2008 at 9:57 PM

    maaaaasssterrrrr….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s