| Measter |
11-13-2008 09:03 PM |
XHTML Strict Table Class
I mentioned this class in my intro thread, and thought I'd put it here for you lot to rip apart.
PHP Code:
<?php /** * Table Class * * Will create an XHTML 1.0 Strict compliant table. * * @author Measter <measter2@yahoo.co.uk> * @version 2.1 */ class Table { private $__bHeader = false; private $__bFooter = false; private $__bCaption = false; private $__aRows = array( 'cap' => array(), 'head' => array(), 'foot' => array(), 'body' => array() ); private $__sType = 'body'; private $__iRowID = array( 'body' => 0, 'head' => 0, 'foot' => 0); private $__aHeadAttrib = array(); private $__aFootAttrib = array(); private $__aBodyAttrib = array(); private $__aRowAttrib = array(); private $__aDebugMSG = array(); private $__bDebug = false; /** * Constructer function. * * @param boolean $bDebugging * - Turns debugging on and off. */ public function __construct($bDebugging = false) { if (is_bool($bDebugging)) { $this->__bDebug = $bDebugging; } } /** * Enter description here... * * @param string $sCap * - String containing the caption contents. * @param uarray $aAttrib *Optional* * - Array that contains the HTML attributes for the caption. * - Example: array( 'id' => 'caption', 'title' => 'caption' ) */ public function setCaption($sCap, $aAttrib = array()) { $this->__bCaption = true; $this->__aRows['cap'][0] = $sCap; if (count($aAttrib) !== 0 && is_array($aAttrib)) { $this->__aRows['cap'][1] = $aAttrib; } elseif (!is_array($aAttrib) && $this->__bDebug === true) { $this->addDebugMSG('Error setting caption attributes: The attribute parameter must be an array.'); } return $this; } /** * Use to set the destination of new cells to the body, and the attributes. * * @param array $aAttrib *Optional* * - Array that contains the HTML attributes for the body. * - Example: array( 'id' => 'table1', 'title' => 'Cell 1' ) */ public function setBody($aAttrib = array()) { $this->__sType = 'body'; if (count($aAttrib) !== 0 && is_array($aAttrib)) { $this->__aBodyAttrib = $aAttrib; } elseif (!is_array($aAttrib) && $this->__bDebug === true) { $this->addDebugMSG('Error setting body attributes: The attribute parameter must be an array.'); } return $this; } /** * Use to set the destination of new cells to the header, and the attributes. * * @param array $aAttrib *Optional* * - Array that contains the HTML attributes for the head. * - Example: array( 'id' => 'table1', 'title' => 'Cell 1' ) */ public function setHead($aAttrib = array()) { $this->__sType = 'head'; $this->__bHeader = true; if (count($aAttrib) !== 0 && is_array($aAttrib)) { $this->__aHeadAttrib = $aAttrib; } elseif (!is_array($aAttrib) && $this->__bDebug === true) { $this->addDebugMSG('Error setting header attributes: The attribute parameter must be an array.'); } return $this; } /** * Use to set the destination of new cells to the footer, and the attributes. * * @param array $aAttrib *Optional* * - Array that contains the HTML attributes for the foot. * - Example: array( 'id' => 'table1', 'title' => 'Cell 1' ) */ public function setFoot($aAttrib = array()) { $this->__sType = 'foot'; $this->__bFooter = true; if (count($aAttrib) !== 0 && is_array($aAttrib)) { $this->__aFootAttrib = $aAttrib; } elseif (!is_array($aAttrib) && $this->__bDebug === true) { $this->addDebugMSG('Error setting footer attributes: The attribute parameter must be an array.'); } return $this; } /** * Adds a cell to the table * * @param string $data * - String that contains the cell contents. * * @param array $attrib *Optional* * - Array that contains the HTML attributes for the cell. * - Will be merged with the row attributes, overwriting existing attributes. * - Example: array( 'id' => 'table1', 'title' => 'Cell 1' ) */ public function addCell($sData, $aAttrib = array()) { if (!is_array($aAttrib)) { $aAttrib = array(); if ($this->__bDebug === true) { $this->addDebugMSG('Error setting cell attributes: The attribute parameter must be an array. Row: ' . $this->__iRowID[$this->__sType] . ', Cell: ' . (count($this->__aRows[$this->__sType][$this->__iRowID[$this->__sType]-1])+1). '.'); } } //Check the destination for the current cell, changing the cell tag in the process because //the w3c decided in their infinite wisdom to give the header cells a differant tag. :( switch ($this->__sType) { case 'head': $sTag = 'th'; $sType = 'head'; $iRowID = $this->__iRowID['head']; $aAttrib = array_merge($this->__aHeadAttrib, $this->__aRowAttrib, $aAttrib); break; case 'body': $sTag = 'td'; $sType = 'body'; $iRowID = $this->__iRowID['body']; $aAttrib = array_merge($this->__aBodyAttrib, $this->__aRowAttrib, $aAttrib); break; case 'foot': $sTag = 'td'; $sType = 'foot'; $iRowID = $this->__iRowID['foot']; $aAttrib = array_merge($this->__aFootAttrib, $this->__aRowAttrib, $aAttrib); break; } $sCell = "\t\t\t<{$sTag} "; //Check if there are attributes to be added. if (count($aAttrib) !== 0) { //Output the attributes. foreach ($aAttrib as $sKey => $sValue) { $sCell .= $sKey . '="' . $sValue . '" '; } } $sCell .= ">{$sData}</{$sTag}>\n"; //Output the cell to the storage array. $this->__aRows[$sType][($iRowID)-1][] = $sCell; return $this; } /** * Adds a new row to the table. Only of use for the body. * * @param array $attrib *Optional* * - Array that contains the HTML attributes for the row. * - Will be merged with the body attributes, overwriting existing attributes. * - Example: array( 'id' => 'table1', 'title' => 'Cell 1' ) */ public function addRow($aAttrib = array()) { switch ($this->__sType) { case 'head': $this->__iRowID['head']++; break; case 'foot': $this->__iRowID['foot']++; break; case 'body': $this->__iRowID['body']++; break; } if (count($aAttrib) === 0) { //Blank the row attributes array $this->__aRowAttrib = array(); } elseif (count($aAttrib) !== 0) { $this->__aRowAttrib = array_merge($this->__aBodyAttrib, $aAttrib); } elseif (!is_array($aAttrib) && $this->__bDebug === true) { $this->addDebugMSG('Error setting row attributes: The attribute parameter must be an array.'); } return $this; } /** * Constructs, and ouputs the table. * * @param array $aAttrib *Optional* * - Array containing the HTML attributes for the table. * - Example: array( 'id' => 'table1', 'title' => 'Table 1' ) */ public function Output($aAttrib = null) { $sTable = "<table "; //Check if the attribute paramenter is set. if ($aAttrib !== null) { //Output the attributes. foreach ($aAttrib as $sKey => $sValue) { $sTable .= $sKey . '="' . $sValue . '" '; } } $sTable .= ">\n"; //Check if the caption is to be displayed. if ($this->__bCaption === true) { $sTable .= "\t<caption "; //Output the caption attributes. if (is_array($this->__aRows['cap'][1])) { foreach ($this->__aRows['cap'][1] as $sKey => $sValue) { $sTable .= $sKey . '="' . $sValue . '" '; } } $sTable .= '>' . $this->__aRows['cap'][0] . "</caption>\n"; } //Check if the header is to be displayed. if ($this->__bHeader === true) { $sTable .= "\t<thead>\n"; //Output the header rows. foreach ($this->__aRows['head'] as $aRow) { $sTable .= "\t\t<tr>\n"; //Output the header cells. foreach($aRow as $sCell) { $sTable .= $sCell; } $sTable .= "\t\t</tr>\n"; } $sTable .= "\t</thead>\n"; } //Check if the footer is to be displayed. if ($this->__bFooter === true) { $sTable .= "\t<tfoot>\n"; //Output the footer rows. foreach ($this->__aRows['foot'] as $aRow) { $sTable .= "\t\t<tr>\n"; //Output the footer cells. foreach($aRow as $sCell) { $sTable .= $sCell; } $sTable .= "\t\t</tr>\n"; } $sTable .= "\t</tfoot>\n"; } $sTable .= "\t<tbody>\n"; //Loop over the rows for the table body. foreach ($this->__aRows['body'] as $aRow) { $sTable .= "\t\t<tr>\n"; //Output the cells for the current row. foreach ($aRow as $sCell) { $sTable .= $sCell; } $sTable .= "\t\t</tr>\n"; } $sTable .= "\t</tbody>\n"; $sTable .= "</table>"; echo $sTable; } /** * Adds a message to the debug message array. * * @param string $sMessage * - String containing the message to add to the debug array. */ private function addDebugMSG($sMessage) { $this->__aDebugMSG[] = (string) $sMessage; } /** * Outputs all debug messages */ public function debugOutput() { if ($this->__bDebug === true) { if (count($this->__aDebugMSG) !== 0) { $sDebugOutput = '<br /><p style="font-weight: bold;">'; foreach ($this->__aDebugMSG as $sDebugMSG) { $sDebugOutput .= $sDebugMSG . "<br />\n"; } $sDebugOutput .= '</p>'; echo $sDebugOutput; } else { echo '<p>No error messages to output.</p><br />'; } } else { echo '<p>Debugging is disabled.</p><br />'; } } } ?>
Example for using it:
PHP Code:
<?php //Include the Table class. require_once('table.class.php'); //Create a new Table object, turn on debuging. $table = new Table(true); //Set the destination of the new cells to the body section, setting the class attribute. $table->setBody(array('class' => 'tableBody0')); //Add a new row to the body. $table->addRow(); //Add a couple cells $table->addCell('Cell 1'); $table->addCell('Cell 2'); //Lets add a second row, this time with a differant style. $table->addRow(array('class' => 'tableBody1')); //Add more cells on the new row, directly inputting the data. $table->addCell('Cell 3'); $table->addCell('Cell 4'); //Now lets chain a third row, Cell 5 using the style set in the setBody function, Cell 6 using a custom set style. $table->addRow()->addCell('Cell 5')->addCell('Cell 6', array('class' => 'tableBody2')); //Setting a fourth row, this time with the 8th cell attribute parameter to a string, to show the error output on debugging. $table->addRow()->addCell('Cell 7')->addCell('Cell 8', 'bleh'); //Set the destination to the header section. $table->setHead(array('class' => 'tableHeadFoot')); //Add cells to the header. $table->addCell('Data'); $table->addCell('Data'); //Second header row. $table->addRow()->addCell('Data')->addCell('Data'); //And the same for the footer section, this time adding the attributes directly to the cells. $table->setFoot(); $table->addCell('Data', array('class' => 'tableHeadFoot')); $table->addCell('Data', array('class' => 'tableHeadFoot')); //And a second row for the footer. Note that these cells don't have any attributes. //This is because there was no style set for the footer, or this row. $table->addRow()->addCell('Data')->addCell('Data'); //Adding a caption. $table->setCaption('Example Table', array('id' => 'caption')); //Time to output the table to the document, the parameter is the style info for the <table> element. $table->Output(array('id' => 'Table')); //Lets output any error messages. $table->debugOutput(); ?>
That'll output a nice little table, and an error message about the 'Cell 8' cell. I created this mostly out of boredom.
For the attributes, you can enter them when you set to Head, Foot or Body, when you add a new body row, or when you add a new cell. attribute priority is Cell > Row > Body/Head/Foot, with higher priority overwriting lower ones.
Be gentle. :-P
*Edit* I've updated the code and the example to the latest.
I'll also add that the caption, header, and footer are not added to the table unless you use the set* functions, so you fon't find empty <caption>, <thead>, or <tfoot> tags.
|