Posted By

mscribellito on 06/06/11


Tagged


Versions (?)

PHP Table Generator


 / Published in: PHP
 

  1. <?php
  2.  
  3. /*
  4.  * @class: Table
  5.  * @author: Michael Scribellito
  6.  * @date: 6-1-11
  7.  */
  8.  
  9. class Table {
  10.  
  11. private $tmpl = array(
  12. 'table' => '<table>',
  13. 'caption' => '<caption>',
  14. '/caption' => '</caption>',
  15. 'thead' => '<thead>',
  16. 'thead>tr' => '<tr>',
  17. 'thead>tr>th' => '<th>',
  18. '/thead>tr>th' => '</th>',
  19. '/thead>tr' => '</tr>',
  20. '/thead' => '</thead>',
  21. 'tbody' => '<tbody>',
  22. 'tbody>tr' => '<tr>',
  23. 'tbody>tr>td' => '<td>',
  24. '/tbody>tr>td' => '</td>',
  25. '/tbody>tr' => '</tr>',
  26. 'tbody.alt>tr' => '<tr class="alt">',
  27. 'tbody.alt>tr>td' => '<td>',
  28. '/tbody.alt>tr>td' => '</td>',
  29. '/tbody.alt>tr' => '</tr>',
  30. '/tbody' => '</tbody>',
  31. '/table' => '</table>',
  32. );
  33. private $caption = NULL;
  34. private $headings = array();
  35. private $rows = array();
  36. private $empty = '&nbsp;';
  37. private $function = FALSE;
  38.  
  39. const NEW_LINE = "\n";
  40.  
  41. public function __construct() {
  42.  
  43. }
  44.  
  45. public function addFilter($function) {
  46.  
  47. for ($i = 0, $l = count($this->rows); $i < $l; $i++) {
  48. call_user_func($function, $this->rows[$i], $i, $this);
  49. }
  50. }
  51.  
  52. public function setTemplate($item, $value = '') {
  53.  
  54. if (!is_array($item)) {
  55.  
  56. if (isset($this->tmpl[$item])) {
  57. $this->tmpl[$item] = $value;
  58. }
  59. } else {
  60.  
  61. foreach ($item as $key => $val) {
  62.  
  63. $this->setTemplate($key, $val);
  64. }
  65. }
  66. }
  67.  
  68. public function setCaption($caption) {
  69.  
  70. $this->caption = (string) $caption;
  71.  
  72. return $this;
  73. }
  74.  
  75. public function setColumnHeadings($headings) {
  76.  
  77. if (is_array($headings)) {
  78. $this->headings = $headings;
  79. } else {
  80. $this->headings = func_get_args();
  81. }
  82.  
  83. return $this;
  84. }
  85.  
  86. public function setEmpty($empty) {
  87.  
  88. $this->empty = (string) $empty;
  89.  
  90. return $this;
  91. }
  92.  
  93. public function setFunction($function) {
  94.  
  95. $this->function = $function;
  96. }
  97.  
  98. public function addRow($row) {
  99.  
  100. if (is_array($row)) {
  101.  
  102. } else if (is_object($row)) {
  103. $row = array_values((array) $row);
  104. } else {
  105. $row = func_get_args();
  106. }
  107. $this->rows[] = $row;
  108.  
  109. return $this;
  110. }
  111.  
  112. public function removeRow($index) {
  113.  
  114. if (is_int($index)) {
  115. unset($this->rows[$index]);
  116. }
  117. }
  118.  
  119. public function addData($data) {
  120.  
  121. if (!is_array($data) || count($data) == 0) {
  122. return FALSE;
  123. }
  124.  
  125. foreach ($data as $row) {
  126. $this->addRow($row);
  127. }
  128. }
  129.  
  130. private function head() {
  131.  
  132. $head = '';
  133.  
  134. if ($this->caption != NULL) {
  135.  
  136. $head .= $this->tmpl['caption'] . $this->caption;
  137.  
  138. $head .= $this->tmpl['/caption'] . self::NEW_LINE;
  139. }
  140.  
  141. if (count($this->headings) > 0) {
  142.  
  143. $head .= $this->tmpl['thead'] . self::NEW_LINE;
  144.  
  145. $head .= $this->tmpl['thead>tr'] . self::NEW_LINE;
  146.  
  147. foreach ($this->headings as $heading) {
  148.  
  149. $head .= $this->tmpl['thead>tr>th'] . $heading;
  150.  
  151. $head .= $this->tmpl['/thead>tr>th'] . self::NEW_LINE;
  152. }
  153.  
  154. $head .= $this->tmpl['/thead>tr'] . self::NEW_LINE;
  155.  
  156. $head .= $this->tmpl['/thead'] . self::NEW_LINE;
  157. }
  158.  
  159. return $head;
  160. }
  161.  
  162. private function body() {
  163.  
  164. $body = '';
  165.  
  166. if (count($this->rows) > 0) {
  167.  
  168. $i = 1;
  169.  
  170. $body .= $this->tmpl['tbody'] . self::NEW_LINE;
  171.  
  172. foreach ($this->rows as $row) {
  173.  
  174. if (!is_array($row)) {
  175. break;
  176. }
  177.  
  178. $alt = $i++ % 2 ? '' : '.alt';
  179.  
  180. $body .= $this->tmpl['tbody' . $alt . '>tr'] . self::NEW_LINE;
  181.  
  182. foreach ($row as $cell) {
  183.  
  184. if ($cell == "" || $cell == NULL) {
  185. $cell = $this->empty;
  186. }
  187.  
  188. if ($this->function != FALSE && is_callable($this->function)) {
  189. $cell = call_user_func($this->function, $cell);
  190. }
  191.  
  192. $body .= $this->tmpl['tbody' . $alt . '>tr>td'] . $cell;
  193.  
  194. $body .= $this->tmpl['/tbody' . $alt . '>tr>td'] . self::NEW_LINE;
  195. }
  196.  
  197. $body .= $this->tmpl['/tbody' . $alt . '>tr'] . self::NEW_LINE;
  198. }
  199.  
  200. $body .= $this->tmpl['/tbody'] . self::NEW_LINE;
  201. }
  202.  
  203. return $body;
  204. }
  205.  
  206. public function generate() {
  207.  
  208. $table = $this->tmpl['table'] . self::NEW_LINE;
  209.  
  210. $table .= $this->head();
  211.  
  212. $table .= $this->body();
  213.  
  214. $table .= $this->tmpl['/table'] . self::NEW_LINE;
  215.  
  216. $this->clear();
  217.  
  218. return $table;
  219. }
  220.  
  221. public function clear() {
  222.  
  223. $this->headings = array();
  224. $this->rows = array();
  225.  
  226. return $this;
  227. }
  228.  
  229. public function __toString() {
  230.  
  231. return $this->generate();
  232. }
  233.  
  234. }

Report this snippet  

You need to login to post a comment.