addHeader($content); * $table->addCaption($caption); * $table->addFooter($content); * $table->addBody($content); * $table->drawTable(); * * Where, * $caption - string Caption for table * $content - array Content for table * array[row][column][attributes] * array[row][attributes] * array[row][column][content] = string * $attributes = array ('attributeName' => 'attributeValue') * @version 1.0 - 2009-05-05 */ class HtmlTable { var $table = ''; var $caption = ''; var $header = array(); var $headerRows = 0; var $headerColumns = 0; var $body = array(); var $bodyRows = 0; var $bodyColumns = 0; var $footer = array(); var $footerRows = 0; var $footerColumns = 0; var $maxColumns = 0; /* On construct we set up the table attributes * for example: class, id, styles, etc */ function __construct($attributes = null) { if (!is_array($attributes) && !is_null($attributes)) { throw new Exception ('Table attributes needs to be an array'); } else { $this->table = $attributes; } } function addHeader($content = null) { if (is_array($content)) { $this->header = $content; } else { throw new Exception ('Header content needs to be an array'); } ksort($this->header); // Find the number of rows and columns in the header $rowColumns = $this->findRowsColumns($this->header); $this->headerRows = $rowColumns[0]; $this->headerColumns = $rowColumns[1]; } function addFooter($content = null) { if (is_array($content)) { $this->footer = $content; } else { throw new Exception ('Footer content needs to be an array'); } ksort($this->footer); // Find the number of rows and columns in the footer $rowColumns = $this->findRowsColumns($this->footer); $this->footerRows = $rowColumns[0]; $this->footerColumns = $rowColumns[1]; } function addBody($content = null) { if (is_array($content)) { $this->body = $content; } else { throw new Exception ('Body content needs to be an array'); } ksort($this->body); // Find the number of rows and columns in the body $rowColumns = $this->findRowsColumns($this->body); $this->bodyRows = $rowColumns[0]; $this->bodyColumns = $rowColumns[1]; } function addCaption($caption = null) { $this->caption = strip_tags($caption); } /* This function generates the HTML for the * table, returninging the HTML as a string. */ function drawTable() { // What was the largest number of columns? $maxColumns = array($this->headerColumns, $this->bodyColumns, $this->footerColumns); $this->maxColumns = max($maxColumns); // Draw the main table 'holder'... $table = 'table) > 0) { foreach ($this->table AS $attributeName => $attributeValue) { $table .= "{$attributeName} = \"{$attributeValue}\" "; } } $table .= ">\n"; // Write the caption if (!empty($this->caption)) { $table .= "\n"; } // Write the table header... $table .= $this->drawHeader(); // Write the table footer... $table .= $this->drawFooter(); // Write the table body... $table .= $this->drawBody(); $table .= "
{$this->caption}
\n"; return $table; } function findRowsColumns($dataArray) { $rows = 0; $columns = 0; if (!is_array($dataArray)) { throw new Exception('Input needs to be an array'); } $rows = array_keys($dataArray); $rows = max($rows)+1; $columns = 0; foreach ($dataArray AS $arrayKey => $arrayRow) { $currentColumns = max(array_keys($arrayRow)); if ($currentColumns > $columns) { $columns = $currentColumns; } } $columns++; return array($rows,$columns); } function drawHtml($dataArray,$rows,$columnTag = 'td', $sectionType = 'tbody') { if (!is_array($this->body) || (count($this->body) == 0)) { throw new Exception('Table body has not been set'); } if ($rows > 0) { $table = "<{$sectionType}>\n"; for ($i=0; $i<$rows; $i++) { $thisRow = $dataArray[$i]; $table .= "\t $attributeValue) { $table .= "{$attributeName} = \"{$attributeValue}\" "; } } $table .= ">\n"; // Write out the table headers for ($j=0; $j < $this->maxColumns; $j++) { $table .= "\t\t<{$columnTag} "; if (isset($thisRow[$j]['attributes']) && is_array($thisRow[$j]['attributes'])) { foreach ($thisRow[$j]['attributes'] AS $attributeName => $attributeValue) { $table .= "{$attributeName} = \"{$attributeValue}\" "; } } $table .= '>'; if (isset($thisRow[$j]['content']) && !empty($thisRow[$j]['content'])) { $table .= $thisRow[$j]['content']; } else { $table .= ' '; } $table .= "\n"; } $table .= "\n"; } $table .= "\n"; } else { $table = ''; } return $table; } function drawHeader() { $table = $this->drawHtml($this->header, $this->headerRows, 'th', 'thead'); return $table; } function drawBody() { $table = $this->drawHtml($this->body, $this->bodyRows, 'td', 'tbody'); return $table; } function drawFooter() { $table = $this->drawHtml($this->footer, $this->footerRows, 'td', 'tfoot'); return $table; } }