mbeals Posted July 25, 2008 Share Posted July 25, 2008 This is my first attempt at OOP programming. It's 3 classes that build and HTML table. My goal was to create something that would let me take an array of data and quickly build a table, yet retain enough granularity to format individual rows and cells. Like I said, it's my first attempt at OOP, so I'm sure there are things that aren't Kosher, and I would appreciate some feedback on those ways to improve and streamline the code. Thought about putting this in the beta testing forum, but it seemed more fitting here. here's the class file: <?php class Cell { /**** Class Cell **************** This class defines the cell of an html table. The constructor and fill methods accept either a single string value, which will become the contents of the cell or an associative array, setting: alignment : alignment background color : color width : width contents : contents formatting tags : format if it's a header cell : headerCell */ private $alignment; private $color; private $width; private $contents; private $format; private $headerCell; private $colspan; function __construct($stuff = NULL) { $this->fill($stuff); } function fill($stuff){ if(is_array($stuff)){ $this->contents = $stuff['contents']; $this->color = $stuff['color']; $this->alignment = $stuff['alignment']; $this->width = $stuff['width']; $this->format = $stuff['format']; }else{ $this->contents = $stuff; } } function setFormat($var){ $this->format = $var; } function isHeader($bool){ $this->headerCell = $bool; } function bgColor($bgcolor) { $this->color = $bgcolor; } function textAlign($align) { $this->alignment = $align; } function setWidth($val){ $this->width = $val; } function setColSpan($val){ $this->colspan = $val; } function appendCell($stuff) { $this->contents .= $stuff; } function clear(){ $this->contents = ''; $this->color = ''; } function getColor() { return $this->color; } function getAlign() { return $this->alignment; } function getWidth(){ return $this->width; } function getContents(){ return $this->contents; } function getHeader(){ return $this->headerCell; } function getAll(){ $info['color'] = $this->getColor(); $info['alignment'] = $this->getAlign(); $info['width'] = $this->getWidth(); $info['contents'] = $this->getContents(); $info['header'] = $this->getHeader(); return $info; } function getHTML(){ if($this->headerCell){ $html = "\t<th "; }else{ $html = "\t<td ";} if($this->width) $html .= 'width="'.$this->width.'" '; if($this->color) $html .= 'bgcolor="'.$this->color.'" '; if($this->colspan) $html .= 'colspan="'.$this->colspan.'" '; if($this->alignment) $html .= 'align="'.$this->alignment.'" '; $html .= '>'; if($this->format) $html .= '<'.$this->format.'> '; $html .= $this->contents; if($this->format) $html .= '</'.$this->format.'> '; if($this->headerCell){ $html .= "</th>\n";}else{ $html .= "</td>\n";} return $html; } function printHTML(){ echo $this->getHTML(); } } ########################################################################################## class Row { /***** Class Row ************************* The Row Class builds html rows and contains methods for interacting with the Cell class. The constructor and fill methods create an array of cell objects which represent the row. These methods will accept an array of cells, where each item is either a previously declaired cell object or the standard input to the cell constructor (see class cell). This class also contains methods for updating cells (contained in $this->cells en-mass, allowing cell level formatting to be applied to an entire row's worth of cells at once. Common css tags, such as id and class can also be assigned to the row by setting the appropriate attributes. */ public $cells; private $alignment; private $valign; private $id; private $class; private $cellSpace; private $headerRow; private $color; function __construct($stuff = NULL) { if($stuff) $this->fill($stuff); } function fill($stuff) { $this->cells = array(); $this->addCells($stuff); return $this->cells; } function addCells($stuff) { foreach($stuff as $cell){ if(is_object($cell)){ $this->cells[] = $cell; }else{ $this->cells[] = new Cell($cell); } } return $this->cells; } function emptyRow() { foreach($this->cells as $cell) $cell->clear(); } function setValign($align){ $this->valign = $align; } function setId($val){ $this->id = $val; } function setClass($val){ $this->class = $val; } function setHeader($bool){ $this->headerRow = $bool; $this->setHeaderDO(); } function setHeaderDO(){ foreach($this->cells as $cell) $cell->isHeader($this->headerRow); } function setSpacing($space){ $this->cellSpace = $space; $this->setSpacingDO(); } function setSpacingDO(){ if(count($this->cells) == count($this->cellSpace)){ for($i=0;$i<count($this->cells);$i++) $this->cells[$i]->setWidth($this->cellSpace[$i]); } } function setColor($color){ $this->color = $color; $this->setColorDO(); } function setColorDO(){ foreach($this->cells as $cell) $cell->bgColor($this->color); } function setAlignment($align){ $this->alignment = $align; $this->setAlignmentDO(); } function setAlignmentDO(){ foreach($this->cells as $cell) $cell->textAlign($this->alignment); } function getHTML(){ $html = '<tr '; if($this->valign) $html .= 'valign="'.$this->valign.'" '; if($this->id) $html .= 'id="'.$this->id.'" '; if($this->class) $html .= 'class="'.$this->class.'" '; $html .= "> \n"; foreach($this->cells as $cell) $html .= $cell->getHTML(); $html .= "</tr>\n"; return $html; } function printHTML(){ echo $this->getHTML(); } } ########################################################################################################### class Table { /****** Class Table *********** This class builds an HTML table by forming an array of rows (defined by the row class) Rows can be added individually or in bulk through the insertRow and insertMRows methods respectively. Both methods will accept either an array (as defined in class Row) or a predefined row object, with the insertMRows method accepting an array of said items. Table attributes are not strict and can be assigned through the setAttribute($attribute, $value) and setAllAttributes($attribute) methods. The setAttribute method accepts two arguments: the attribute (tag) to set and it's value, while the setAllAttributes method accepts an associative array where the key is the attribute (tag) and the value is the value Zebra Striping can also be performed with the stripe() method which takes the color of the stripe as an argument. */ public $rows; private $cellSpacing; private $Attributes; private $zebra; function __construct($stuff = NULL) { if($stuff)insertMRows($stuff); } function insertMRows($stuff) { foreach($stuff as $row){ if(is_object($row)) $this->rows[] = $row; if(is_array($row)) $this->rows[] = new Row($row); } return $this->rows; } function insertRow($row) { if(is_array($row)){ $newRow = new Row($row); $this->rows[] = $newRow; return $newRow; } if(is_object($row)) $this->rows[] = $row; return $row; } function stripe($color){ $this->zebra = $color; $this->stripeDo(); } function stripeDo(){ $i = 0; if($this->rows){ foreach($this->rows as $row){ if($i%2) $row->setColor($this->zebra); $i++; } } } function setAllAttributes($stuff) { $this->Attributes = array(); foreach($stuff as $att => $value) $this->Attributes[$att] = $value; } function setAttribute($att, $value) { $this->Attributes[$att] = $value; } function setSpacing($spaceArr){ $this->cellSpacing = $spaceArr; $this->setSpacingDo(); } function setSpacingDo(){ foreach($this->rows as $row) $row->setSpacing(''); $this->rows[0]->setSpacing($this->cellSpacing); } function getHTML(){ $html = '<table '; if($this->Attributes){ foreach($this->Attributes as $attribute => $value) $html .= $attribute.'="'.$value.'" '; } $html .= ">\n"; if($this->rows) foreach($this->rows as $row) $html .= "\n".$row->getHTML(); $html .="</table>"; return $html; } function printHTML(){ echo $this->getHTML(); } } ################################################################################################################# ?> And here is a demo of how to use it <?php include 'TableClass.php'; $Table = new Table; ### build table all at once ###### $stuff = array( array(0,1,2,3), array(1,2,3,4), array(2,2,3,4), array(3,2,3,4), array(4,2,3,4), array(5,2,3,4)); $Table->insertMRows($stuff); #### Set a bunch of attributes ############ $attributes = array( 'width' => '80%', 'align' => 'center'); $Table->setAllAttributes( $attributes); #### Set a Single Attribute ############# $Table->setAttribute('border', 1); #### Zebra Stripe the Table ########## $Table->stripe('yellow'); ###Set the Table spacing (old school) ##### $Table->setSpacing(array(10,10,10,60)); #### Add a header Row ############# $Table->rows[0]->setHeader(1); ########## Row functions ########################## ### Add another row $cellA = new Cell('This is a test'); $CellB = new Cell( array('contents' => 'This is also a Test', 'color' => 'blue', 'format' => 'h1', 'alignment' => 'center')); $newRow = new Row(array('blank', $cellA, 'blank', $CellB)); $myRow = $Table->insertRow($newRow); $myRow->setValign('Top'); ### Cell functions $myRow->cells[0]->textAlign('center'); $newCell = new Cell; $newCell-> fill('This is a Spanned Cell'); $newCell-> setColSpan('4'); $Table->insertRow(array($newCell)); $Table->rows[7]->cells[0]->textAlign('center'); $Table->printHTML(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/116604-does-this-look-okay/ Share on other sites More sharing options...
ignace Posted July 27, 2008 Share Posted July 27, 2008 why not just use a row object? the memory usage will be significantly lower when only using a row object, instead of a cell object, you have much more cells then you have rows Quote Link to comment https://forums.phpfreaks.com/topic/116604-does-this-look-okay/#findComment-600880 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.