 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
11-13-2008, 09:03 PM
|
#1 (permalink)
|
|
The Wanderer
Join Date: Nov 2008
Location: Plymouth, UK
Posts: 9
Thanks: 0
|
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.
*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.
Last edited by Measter : 11-14-2008 at 02:57 PM.
|
|
|
|
11-14-2008, 06:16 AM
|
#2 (permalink)
|
|
The Contributor
Join Date: Oct 2008
Location: Nuremberg, Germany
Posts: 26
Thanks: 3
|
Great work, but there are two things missing:
* the table caption
* the possibilty to add a <th></th> to a row in tbody and tfoot
btw: th is allowed in tr, tr is allowed in tbody, thead, tfoot and table tag.
__________________
|
|
|
11-14-2008, 01:48 PM
|
#3 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
That's very nice! Although 4 posts in and TalkPHP has already converted you to using the Hungarian notation? Where is your resilience, the "I shan't do that!". Were you already using it before you joined the community?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
11-14-2008, 02:37 PM
|
#4 (permalink)
|
|
The Wanderer
Join Date: Nov 2008
Location: Plymouth, UK
Posts: 9
Thanks: 0
|
Actually I was already doing
PHP Code:
if (something)
{
do something
}
for the brackets, but realised through reading here that doing it that way is easier to read.
For the variable names, I started doing that after reading this forum, and reading some crit on other scripts about good variable names.
Awuehr, I completly forgot about the caption, thanks for reminding me, I've got it in and working now.
About the <th> element, according to the W3C page, the <th> element is only for the header, although it doesn't prevent validation if it's in the foot or body.
Still trying to get the second row of a header or footer working, having a little trouble outputting it.
*Edit* OK, found out why i was having trouble, I was outputting the footer cells in the header, and not the header cells. The footer cells were outputting correctly, I just hadn't added a second row. Yay for copy+paste... 
Last edited by Measter : 11-14-2008 at 02:59 PM.
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|