This snippet of code can be used to build dynmic tables and format each cell with a defined style.
Its current state is very basic, but you could easily extend it to support much more.
Here is the basic code first, then I will show an example of how to use it.
// start the table
function s($width) {
$this->final = '<table width="'.$width.'" cellpadding="0" cellspacing="0">';
}
// start a new row
function r($content, $style) {
$this->final .= '<tr class="box'.$style.'"><td>'.$content.'</td></tr>';
}
// complete and return the table
function finish() {
return $this->final .= '</table><br />';
}
}
?>
Example:
<?
$tab = new box; // creat new object
$tab->s("135"); // start the table with width of 135
$tab->r("this is my header", 1); // create row with using style 1
$tab->r("this is my sub", 2); // create row using style 2
$tab->r("this is my sub", 2); // create row using style 2
$tab->r("this is my sub", 2); // create row using style 2
$multiline = "this is a multi-line<br/>example."; // create a variable first before adding to new
row
$tab->r($multiline, 2); // add the variable to create new row
echo $tab->finish(); // now print the table to screen
unset($tab); // kill object
?>
kenji wrote :943
I have one question. Could you explain why you would use stylesheets to build a table when the stylesheet could be used to create your page using <div> and <span> tags with greater speed and less code?
Stuart Cochrane wrote :944
Well as with everything in programming - people prefer to do things their own way.
This example was designed to show how easy it is to add styles to individual table elements and states that it can easily be extended to fit your own needs.
Thanks for your comments anyway, critism is always good!!
kenji wrote :957
No worries. A basic table will always be needed in case a user uses their own stylesheets ... for now.