Please give comments to the table class I created. It definitely needs some improvement, but I am guessing it is quite useful to construct simple tables, especially for tables used to show database results:
The class file:
<?php
class Table {
public $tablename;
public $columns = array();
public $columnwidth;
public $tablewidth;
public $style;
public $alignment;
public $cells = array();
public $background = array();
public $content;
public function __construct($tablename, $columns, $columnwidth = "", $tablewidth = "", $background = ""){
// Create the table
$this->tablename = $tablename;
if(!is_array($columns)) die("Cannot create a table with only one column...");
if(is_array($columnwidth) and count($columns) != count($columnwidth)) die("Specification of column width is incorrect, please report this to administrator.");
$this->columns = $columns;
if(is_array($columnwidth)){
foreach($columnwidth as $width){
$this->columnwidth[] = "width='{$width}'";
}
}
elseif(!empty($columnwidth) and !is_array($columnwidth)) $this->columnwidth = " width='{$columnwidth}'";
else $this->columnwidth = "";
$this->tablewidth = $tablewidth;
$this->background = $background;
$this->content = "<br>";
}
public function createtable($alignment = "", $style = "", $border = "yes"){
// This method builds the interface of the table, it must be chained with showtable() or buildtable()
$this->alignment = $alignment;
$this->style = $style;
$this->columns = $this->formattable($this->columns);
// Construct the basic table structure
$this->content .= "</br><table";
$this->content .= (!empty($this->tablewidth))?" width='{$this->width}'":"";
$this->content .= ($border == "yes")?" border='1'":"";
$this->content .= (!empty($this->background))?" {$this->background[0]}='{$this->background[1]}'>":">";
// Create the very first row of table
$i = 0;
$this->content .= "<tr>";
foreach($this->columns as $column){
$this->content .= (is_array($this->columnwidth))?"<td {$this->columnwidth[$i]}>{$column}</td>":"<td {$this->columnwidth}>{$column}</td>";
$i++;
}
$this->content .= "</tr>";
return $this;
}
public function buildtable($cells, $alignment = "", $style = ""){
// This method will build the body of our table, it must be chained with showtable() or endtable()
$this->content .= "<tr>";
$this->alignment = (!empty($alignment))?$alignment:$this->alignment;
$this->style = (!empty($style))?$style:$this->style;
$this->cells = $this->formattable($cells);
// Now it is time to construct a row of our table
$i = 0;
foreach($this->cells as $cell){
$this->content .= "<td>{$cell}</td>";
$i++;
}
$this->content .= "</tr>";
return $this;
}
private function formattable($text){
// Thie method formats the content of tables with alignment or style, can only be called witin the table class
if(!is_array($text)) die("Cannot format the table content.");
if(!empty($this->style)){
if(is_array($this->style) and count($this->style) != count($text)) die("Cannot specify the style of table columns...");
for($i = 0; $i < count($text); $i++){
$text[$i] = (is_array($this->style))?"<{$this->style[$i]}>{$text[$i]}</{$this->style[$i]}>":"<{$this->style}>{$text[$i]}</{$this->style}>";
}
}
if(!empty($this->alignment)){
if(is_array($this->alignment) and count($this->alignment) != count($text)) die("Cannot specify alignment of table columns...");
for($i = 0; $i < count($text); $i++){
$text[$i] = (is_array($this->alignment))?"<{$this->alignment[$i]}>{$text[$i]}</{$this->alignment[$i]}>":"<{$this->alignment}>{$text[$i]}</{$this->alignment}>";
}
}
return $text;
}
public function showtable(){
// This method allows the page to show an unfinished table, incase control blocks, loops or forms need to be used
$content = $this->content;
unset($this->content);
return $content;
}
public function endtable(){
// This method terminates the construction of a table
$this->content .= "</table>";
return $this->content;
}
}
?>
Here is an example:
<?php
include("classes/class_tables.php");
$content = "";
$table = new Table("Mytable", array("Column1", "Column2", "Column3", "Column4"), 100, 400, array("bgcolor", "#FFA500"));
$content .= $table->createtable("left")->showtable();
$content .= $table->buildtable(array("r1c1","r1c2","r1c3","r1c4"), "center", "strong")->showtable();
$content .= $table->buildtable(array("r2c1", "r2c2", "r2c3", "r2c4"), "left", "u")
->buildtable(array("r3c1", "r3c2", "r3c3", "r3c4"), "center", "li")
->showtable();
$content .= $table->buildtable(array("r4c1", "r4c2", "r4c3", "r4c4"), "left", "strike")->endtable();
echo $content;
?>
The example gives a table that looks like this:
http://oi41.tinypic.com/2gt4yvl.jpg
So what do you think? Please do not hesitate to point out any problems you see from the codes, Id like to improve it as much as I can before using it in the applications of mine. Comments please?