/ Published in: PHP
Expand |
Embed | Plain Text
<?php /* * @class: Table * @author: Michael Scribellito * @date: 6-1-11 */ class Table { 'table' => '<table>', 'caption' => '<caption>', '/caption' => '</caption>', 'thead' => '<thead>', 'thead>tr' => '<tr>', 'thead>tr>th' => '<th>', '/thead>tr>th' => '</th>', '/thead>tr' => '</tr>', '/thead' => '</thead>', 'tbody' => '<tbody>', 'tbody>tr' => '<tr>', 'tbody>tr>td' => '<td>', '/tbody>tr>td' => '</td>', '/tbody>tr' => '</tr>', 'tbody.alt>tr' => '<tr class="alt">', 'tbody.alt>tr>td' => '<td>', '/tbody.alt>tr>td' => '</td>', '/tbody.alt>tr' => '</tr>', '/tbody' => '</tbody>', '/table' => '</table>', ); private $caption = NULL; private $empty = ' '; private $function = FALSE; const NEW_LINE = "\n"; public function __construct() { } public function addFilter($function) { } } public function setTemplate($item, $value = '') { $this->tmpl[$item] = $value; } } else { foreach ($item as $key => $val) { $this->setTemplate($key, $val); } } } public function setCaption($caption) { $this->caption = (string) $caption; return $this; } public function setColumnHeadings($headings) { $this->headings = $headings; } else { } return $this; } public function setEmpty($empty) { $this->empty = (string) $empty; return $this; } public function setFunction($function) { $this->function = $function; } public function addRow($row) { } else { } $this->rows[] = $row; return $this; } public function removeRow($index) { } } public function addData($data) { return FALSE; } foreach ($data as $row) { $this->addRow($row); } } private function head() { $head = ''; if ($this->caption != NULL) { $head .= $this->tmpl['caption'] . $this->caption; $head .= $this->tmpl['/caption'] . self::NEW_LINE; } $head .= $this->tmpl['thead'] . self::NEW_LINE; $head .= $this->tmpl['thead>tr'] . self::NEW_LINE; foreach ($this->headings as $heading) { $head .= $this->tmpl['thead>tr>th'] . $heading; $head .= $this->tmpl['/thead>tr>th'] . self::NEW_LINE; } $head .= $this->tmpl['/thead>tr'] . self::NEW_LINE; $head .= $this->tmpl['/thead'] . self::NEW_LINE; } return $head; } private function body() { $body = ''; $i = 1; $body .= $this->tmpl['tbody'] . self::NEW_LINE; foreach ($this->rows as $row) { break; } $alt = $i++ % 2 ? '' : '.alt'; $body .= $this->tmpl['tbody' . $alt . '>tr'] . self::NEW_LINE; foreach ($row as $cell) { if ($cell == "" || $cell == NULL) { $cell = $this->empty; } } $body .= $this->tmpl['tbody' . $alt . '>tr>td'] . $cell; $body .= $this->tmpl['/tbody' . $alt . '>tr>td'] . self::NEW_LINE; } $body .= $this->tmpl['/tbody' . $alt . '>tr'] . self::NEW_LINE; } $body .= $this->tmpl['/tbody'] . self::NEW_LINE; } return $body; } public function generate() { $table = $this->tmpl['table'] . self::NEW_LINE; $table .= $this->head(); $table .= $this->body(); $table .= $this->tmpl['/table'] . self::NEW_LINE; $this->clear(); return $table; } public function clear() { return $this; } public function __toString() { return $this->generate(); } }
You need to login to post a comment.
