I love this little class. It probably doesn't seem a big thing at first, but once you build a table using this class you will never look back. I wrote this class because I was tired of counting rows and cells and trying to keep a mental image of everything that was going on. I wanted to be able to see in an instant where my rows were, and what was going on in each cell. When you get involved with nested tables the beauty of this class becomes even more apparent. This class will make your tables:
Creates a new table object
$vars | A '|' delimited string of table attributes and their values. No quotes needed. |
Example | $tb = new Table('class=center'); |
This method starts a new row in your table.
$align | This var will align ('left', 'center', or 'right') your content within every cell across the board. If you want to center every cell in the row then it's easier to say so once here, then to add 'align=center' to every cell in the row hereafter. |
$bgcolor | To color your row a different color you can enter that value here. ('#A5A5A5' or 'gray', etc.) |
Returns | A string of code to add to your $html. |
Example | $html .= $tb->row('center'); |
This method opens a new cell, but does not close ('</td>') it. That happens when you open another cell, create a new row, or close out the form.
$vars | A '|' delimited string of cell attributes and their values. No quotes needed. |
$content | What you want to display inside your cell. This is optional because everything you add to your $html after this will go inside this cell. |
Returns | A string of code to add to your $html. |
Example | $html .= $tb->cell('colspan=2|valign=bottom', 'I span two cells.'); $html .= $tb->cell(); $html .= 'I am a normal cell, and this text will still be contained within it.'; |
This will close out your table and everything else that is open.
Returns | A string of code to add to your $html. |
Example | $html .= $tb->close(); |
<?php /* * author: Kyle Gadd * documentation: http://www.php-ease.com/classes/table.html * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ class Table { private $tableOpen = ''; // so we can save the opening statement for later private $rowOpen = false; // after the first row in a table this $var stays true private $cellOpen = false; // after the first cell in a row this $var stays true private $rowAlign = ''; // to align all of the cells in a row private static $count = 0; function __construct ($vars='') { self::$count++; $this->tableOpen = $this->indent() . '<table' . $this->values($vars) . '>'; } public function row ($align='', $bgcolor='') { // (across the board in every cell) $add = ''; if (!$this->rowOpen) $add .= $this->tableOpen; if ($this->cellOpen && $this->rowOpen) { $add .= '</td></tr>'; $this->rowAlign = ''; } if (!empty($align)) $this->rowAlign = $align; $this->rowOpen = true; $this->cellOpen = false; if (!empty($bgcolor)) $bgcolor = ' bgcolor="' . $bgcolor . '"'; return $add . $this->indent() . '<tr' . $bgcolor . '>'; } public function cell ($vars='', $content='') { $add = ''; if (!$this->rowOpen) $add .= $this->row(); if ($this->cellOpen) $add .='</td>'; $this->cellOpen = true; return $add . $this->indent() . '<td' . $this->values($vars) . '>' . $content; } public function close () { $add = ''; if (!$this->cellOpen) $add .= $this->cell(); $add .= '</td></tr>' . $this->indent() . '</table>'; self::$count--; return $add; } private function values ($vars) { $put = array(); if (!empty($vars)) { $values = explode('|', $vars); foreach ($values as $value) { $pieces = explode('=', $value); $put[array_shift($pieces)] = implode('=', $pieces); } } if (!empty($this->rowAlign)) $put['align'] = $this->rowAlign; $vars = ''; if (!empty($put)) { foreach ($put as $key => $value) { $vars .= ' ' . $key . '="' . $value . '"'; } } return $vars; } private function indent () { $indent = "\n"; for ($i=0; $i<self::$count; $i++) { $indent .= ' '; } return $indent; } } ?>