login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Handige scripts > HTML Table Class

HTML Table Class

Auteur: Gerard - 28 mei 2009 - 01:15 - Gekeurd door: Koen - Hits: 3189 - Aantal punten: 5.00 (1 stem)



Onderstaande classes kunnen gebruikt worden om een HTML tabel op te maken met behulp van OOP PHP. We hebben in principe 3 basis classes tot onze beschikking:
- Html_Table
- Html_Table_Row
- Html_Table_Column

Elk object kan voorzien worden van attributen. Op deze manier is het mogelijk om de tabel volledig naar eigen wens aan te passen. Een voorbeeld van een simpele implementatie wordt getoond onder de code van de classes.

Code:
Html_Table
  1. /**
  2.   * The Html_Table class can be used to build an HTML table using PHP's OOP principles. Each generated
  3.   * table can be divided in three different sections: thead, tbody and tfoot. The class uses fluent
  4.   * interfaces.
  5.   *
  6.   * @author Gerard Klomp <gerard.klomp@sitemasters.be>
  7.   * @version 1.0
  8.   * @license http://sitemasters.be/mit-license.txt MIT License
  9.   */
  10. class Html_Table
  11. {
  12. private $_attributes;
  13. private $_head;
  14. private $_body;
  15. private $_foot;
  16.  
  17. public function __construct()
  18. {
  19. $this->_attributes = array();
  20. $this->_head = array();
  21. $this->_body = array();
  22. $this->_foot = array();
  23. }
  24.  
  25. /**
  26.   * This method allows to add a row to a section of the table. The first argument must be either a
  27.   * string providing the name of the datasection (head, body, foot) or an Html_Table_Row object. If
  28.   * supplied with an Html_Table object this method will return itself, presenting the user with the
  29.   * option to make use of fluent interfaces. If this method is supplied with datasection string it
  30.   * will return the generated Html_Table_Row object.
  31.   *
  32.   * @param string|object $dataSection
  33.   * @param array $attributes
  34.   * @return Html_Table_Row|Html_Table
  35.   */
  36. public function addRow($dataSection, $attributes = array())
  37. {
  38. if (is_object($dataSection) && $dataSection instanceof Html_Table_Row)
  39. {
  40. $section = '_' . $dataSection->section;
  41. array_push($this->{$section}, $dataSection);
  42. return $this;
  43. }
  44.  
  45. $section = '_' . $dataSection;
  46. $i = count($this->{$section});
  47.  
  48. array_push($this->{$section}, New Html_Table_Row($dataSection));
  49.  
  50. foreach ($attributes as $identifier => $value)
  51. {
  52. $this->{$section}[$i]->addAttribute($identifier, $value);
  53. }
  54.  
  55. return $this->{$section}[$i];
  56. }
  57.  
  58. /**
  59.   * This method allows to add an attribute to the table. Each attribute will be applied to the table
  60.   * and not to rows/columns. Examples of such attributes are style, class, border, etc.
  61.   *
  62.   * @param string $identifier
  63.   * @param string $value
  64.   * @return Html_Table
  65.   */
  66. public function addAttribute($identifier, $value)
  67. {
  68. $this->_attributes[$identifier] = $value;
  69. return $this;
  70. }
  71.  
  72. /**
  73.   * This method provides access to create or retrieve attributes based on the identifier. If a value
  74.   * is not supplied the attribute with $identifier will be returned. If both $identifer and $value
  75.   * are supplied it adds the attribute to the array.
  76.   *
  77.   * @param string $identifier
  78.   * @param string $value
  79.   * @return string|Html_Table
  80.   */
  81. public function attr($identifier, $value = null)
  82. {
  83. return is_null($value) ? $this->_attributes[$identifier] : $this->addAttribute($identifier, $value);
  84. }
  85.  
  86. /**
  87.   * Retrieve the HTML for the complete table. If $tidy resolves to True the generated HTML will be
  88.   * indented with the use of tabs. If $tabsToSpaces is set these tabs will be replaced with the number
  89.   * of spaces provided.
  90.   *
  91.   * @param boolean $tidy
  92.   * @param integer $tabsToSpaces
  93.   * @return string
  94.   */
  95. public function getHtml($tidy = true, $tabsToSpaces = null)
  96. {
  97. $attributes = '';
  98. $htmlOutput = '';
  99.  
  100. foreach ($this->_attributes as $identifier => $value)
  101. {
  102. $attributes.= ' ' . $identifier . '="' . $value . '"';
  103. }
  104.  
  105. $htmlOutput = '<table' . $attributes . '>' . "\n";
  106.  
  107. if (count($this->_head) > 0)
  108. {
  109. $htmlOutput .= "\t" . '<thead>' . "\n";
  110.  
  111. foreach($this->_head as $tableHeadRow)
  112. {
  113. $htmlOutput .= $tableHeadRow->getHtml();
  114. }
  115.  
  116. $htmlOutput .= "\t" . '</thead>' . "\n";
  117. }
  118.  
  119. if (count($this->_body) > 0)
  120. {
  121. $htmlOutput .= "\t" . '<tbody>' . "\n";
  122.  
  123. foreach($this->_body as $tableBodyRow)
  124. {
  125. $htmlOutput .= $tableBodyRow->getHtml();
  126. }
  127.  
  128. $htmlOutput .= "\t" . '</tbody>' . "\n";
  129. }
  130.  
  131. if (count($this->_foot) > 0)
  132. {
  133. $htmlOutput .= "\t" . '<tfoot>' . "\n";
  134.  
  135. foreach($this->_foot as $tableFootRow)
  136. {
  137. $htmlOutput .= $tableFootRow->getHtml();
  138. }
  139.  
  140. $htmlOutput .= "\t" . '</tfoot>' . "\n";
  141. }
  142.  
  143. $htmlOutput .= '</table>';
  144.  
  145. if (!$tidy)
  146. {
  147. $htmlOutput = str_replace(array("\t", "\n"), '', $htmlOutput);
  148. }
  149. else if (!is_null($tabsToSpaces) && is_numeric($tabsToSpaces))
  150. {
  151. $htmlOutput = str_replace("\t", str_repeat(' ', $tabsToSpaces), $htmlOutput);
  152. }
  153.  
  154. return $htmlOutput;
  155. }
  156.  
  157. /**
  158.   * Magic method
  159.   *
  160.   * @param string $identifier
  161.   * @param string $value
  162.   */
  163. public function __set($identifier, $value)
  164. {
  165. $this->addAttribute($identifier, $value);
  166. }
  167.  
  168. /**
  169.   * Magic method. If the head, body or foot are requested it will return the array containing the
  170.   * Html_Table_Row elements for the requested section. It is also possible to return the complete
  171.   * array with attributes. If there is an attribute available with the identifier provided it will
  172.   * return this. Else the method will return false.
  173.   *
  174.   * @param string $identifier
  175.   * @return mixed
  176.   */
  177. public function __get($identifier)
  178. {
  179. Switch ($identifier)
  180. {
  181. case 'head':
  182. case 'body':
  183. case 'foot':
  184. case 'attributes':
  185. $identifier = '_' . $identifier;
  186. return $this->{$identifier};
  187. break;
  188.  
  189. default:
  190. if (isset($this->_attributes[$identifier]))
  191. {
  192. return $this->_attributes[$identifier];
  193. }
  194.  
  195. return false;
  196. }
  197. }
  198.  
  199. /**
  200.   * Magic method
  201.   *
  202.   * @return string
  203.   */
  204. public function __toString()
  205. {
  206. return $this->getHtml();
  207. }
  208. }


Html_Table_Row
  1. /**
  2.   * The Html_Table_Row class can be used to instantiate objects representing rows in an HTML table.
  3.   *
  4.   * @author Gerard Klomp <gerard.klomp@sitemasters.be>
  5.   * @version 1.0
  6.   * @license http://sitemasters.be/mit-license.txt MIT License
  7.   */
  8. class Html_Table_Row
  9. {
  10. private $_attributes;
  11. private $_columns;
  12. private $_section;
  13.  
  14. /**
  15.   * Each table row element must specify if it is part of the head, body of foot of the table.
  16.   *
  17.   * @param string $section
  18.   */
  19. public function __construct($section = 'body')
  20. {
  21. $this->_attributes = array();
  22. $this->_columns = array();
  23. $this->_section = $section;
  24. }
  25.  
  26. /**
  27.   * This method allows to add a column to a row of the table. The first argument must be either a
  28.   * string providing the content of the column or and Html_Table_Column object. This method will
  29.   * return itself, presenting the user with the option to make use of fluent interfaces. If the
  30.   * current row is part of the head the column will be automaticly converted to a th, else it will
  31.   * remain the default td.
  32.   *
  33.   * @param string|Html_Table_Column $data
  34.   * @param array $attributes
  35.   * @return Html_Table_Column|Html_Table_Row
  36.   */
  37. public function addColumn($data, $attributes = array())
  38. {
  39. if (is_object($data) && $data instanceof Html_Table_Column)
  40. {
  41. $data->type = $this->_section == 'head' ? 'th' : 'td';
  42. $this->_columns[] = $data;
  43. return $this;
  44. }
  45.  
  46. $i = count($this->_columns);
  47. $this->_columns[$i] = New Html_Table_Column($data);
  48. $this->_columns[$i]->type = $this->_section == 'head' ? 'th' : 'td';
  49.  
  50. foreach ($attributes as $identifier => $value)
  51. {
  52. $this->_columns[$i]->addAttribute($identifier, $value);
  53. }
  54.  
  55. return $this;
  56. }
  57.  
  58. /**
  59.   * Updates the type of all columns belonging to this row to the represented value. HTML valid options
  60.   * are th and td. Can be used to force header columns as a normal column, else it will be automaticly
  61.   * converted to a header column.
  62.   *
  63.   * @param string $type
  64.   * @return Html_Table_Row
  65.   */
  66. public function setColumnType($type)
  67. {
  68. foreach($this->columns as $column)
  69. {
  70. $column->type = $type;
  71. }
  72.  
  73. return $this;
  74. }
  75.  
  76. /**
  77.   * This method allows to add an attribute to the table row. Each attribute will be applied to the
  78.   * table row and not to columns. Examples of such attributes are style, class, border, etc.
  79.   *
  80.   * @param string $identifier
  81.   * @param string $value
  82.   * @return Html_Table_Row
  83.   */
  84. public function addAttribute($identifier, $value)
  85. {
  86. $this->_attributes[$identifier] = $value;
  87. return $this;
  88. }
  89.  
  90. /**
  91.   * This method provides access to create or retrieve attributes based on the identifier. If a value
  92.   * is not supplied the attribute with $identifier will be returned. If both $identifer and $value
  93.   * are supplied it adds the attribute to the array.
  94.   *
  95.   * @param string $identifier
  96.   * @param string $value
  97.   * @return string|Html_Table_Row
  98.   */
  99. public function attr($identifier, $value = null)
  100. {
  101. return is_null($value) ? $this->_attributes[$identifier] : $this->addAttribute($identifier, $value);
  102. }
  103.  
  104. /**
  105.   * Retrieve the HTML for the complete table row.
  106.   *
  107.   * @return string
  108.   */
  109. public function getHtml()
  110. {
  111. $attributes = '';
  112. $htmlOutput = '';
  113.  
  114. foreach ($this->_attributes as $identifier => $value)
  115. {
  116. $attributes.= ' ' . $identifier . '="' . $value . '"';
  117. }
  118.  
  119. $htmlOutput = "\t\t" . '<tr' . $attributes . '>' . "\n";
  120.  
  121. foreach($this->_columns as $column)
  122. {
  123. $htmlOutput .= $column->getHtml();
  124. }
  125.  
  126. return $htmlOutput . "\t\t" . '</tr>' . "\n";
  127. }
  128.  
  129. /**
  130.   * Magic method. It offers the option to update the section to which the row belongs or to set a
  131.   * single attribute.
  132.   *
  133.   * @param string $identifier
  134.   * @param string $value
  135.   */
  136. public function __set($identifier, $value)
  137. {
  138. Switch ($identifier)
  139. {
  140. case 'section':
  141. $this->_section = $value;
  142. $this->setColumnType($value == 'head' ? 'th' : 'td');
  143. break;
  144.  
  145. default:
  146. $this->addAttribute($identifier, $value);
  147. }
  148. }
  149.  
  150. /**
  151.   * Magic method. It offers the option to retrieve all attributes, columns or the name of the current
  152.   * section. If a string is provided it will also check if there is a single attribute available with
  153.   * this identifier.
  154.   *
  155.   * @param string $identifier
  156.   * @return mixed
  157.   */
  158. public function __get($identifier)
  159. {
  160. Switch ($identifier)
  161. {
  162. case 'attributes':
  163. case 'columns':
  164. case 'section':
  165. $identifier = '_' . $identifier;
  166. return $this->{$identifier};
  167. break;
  168.  
  169. default:
  170. if (isset($this->_attributes[$identifier]))
  171. {
  172. return $this->_attributes[$identifier];
  173. }
  174.  
  175. return false;
  176. }
  177. }
  178.  
  179. /**
  180.   * Magic method
  181.   *
  182.   * @return string
  183.   */
  184. public function __toString()
  185. {
  186. return $this->getHtml();
  187. }
  188. }


Html_Table_Column
  1. /**
  2.   * The Html_Table_Column class can be used to instantiate objects representing columns in an HTML table.
  3.   *
  4.   * @author Gerard Klomp <gerard.klomp@sitemasters.be>
  5.   * @version 1.0
  6.   * @license http://sitemasters.be/mit-license.txt MIT License
  7.   */
  8. class Html_Table_Column
  9. {
  10. private $_attributes;
  11. private $_value;
  12. private $_type;
  13.  
  14. /**
  15.   * Each table column element has to have data. It is allowed to supply an empty string. The default
  16.   * type for a new column is td.
  17.   *
  18.   * @param string $section
  19.   */
  20. public function __construct($value)
  21. {
  22. $this->_attributes = array();
  23. $this->_value = $value;
  24. $this->_type = 'td';
  25. }
  26.  
  27. /**
  28.   * This method allows to add an attribute to the table column. Examples of such attributes are style,
  29.   * class, border, etc.
  30.   *
  31.   * @param string $identifier
  32.   * @param string $value
  33.   * @return Html_Table_Column
  34.   */
  35. public function addAttribute($identifier, $value)
  36. {
  37. $this->_attributes[$identifier] = $value;
  38. return $this;
  39. }
  40.  
  41. /**
  42.   * This method provides access to create or retrieve attributes based on the identifier. If a value
  43.   * is not supplied the attribute with $identifier will be returned. If both $identifer and $value
  44.   * are supplied it adds the attribute to the array.
  45.   *
  46.   * @param string $identifier
  47.   * @param string $value
  48.   * @return string|Html_Table_Column
  49.   */
  50. public function attr($identifier, $value = null)
  51. {
  52. return is_null($value) ? $this->_attributes[$identifier] : $this->addAttribute($identifier, $value);
  53. }
  54.  
  55. /**
  56.   * Retrieve the HTML for the table column.
  57.   *
  58.   * @return string
  59.   */
  60. public function getHtml()
  61. {
  62. $attributes = '';
  63.  
  64. foreach ($this->_attributes as $identifier => $value)
  65. {
  66. $attributes.= ' ' . $identifier . '="' . $value . '"';
  67. }
  68.  
  69. return "\t\t\t" . '<' . $this->_type . $attributes . '>'
  70. . $this->_value
  71. . '</' . $this->_type . '>' . "\n";
  72. }
  73.  
  74. /**
  75.   * Magic method. It offers the option to update the type of the column and the content. It also
  76.   * offers the option to add a single attribute to the column.
  77.   *
  78.   * @param string $identifier
  79.   * @param string $value
  80.   */
  81. public function __set($identifier, $value)
  82. {
  83. Switch($identifier)
  84. {
  85. case 'type':
  86. $this->_type = $value;
  87. break;
  88.  
  89. case 'value':
  90. $this->_value = $value;
  91. break;
  92.  
  93. default:
  94. $this->addAttribute($identifier, $value);
  95. }
  96. }
  97.  
  98. /**
  99.   * Magic method. It offers the option to retrieve all attributes, typ or the data of the current
  100.   * column. If a string is provided it will also check if there is a single attribute available with
  101.   * this identifier.
  102.   *
  103.   * @param string $identifier
  104.   * @return mixed
  105.   */
  106. public function __get($identifier)
  107. {
  108. Switch ($identifier)
  109. {
  110. case 'attributes':
  111. case 'type':
  112. case 'value':
  113. $identifier = '_' . $identifier;
  114. return $this->{$identifier};
  115. break;
  116.  
  117. default:
  118. if (isset($this->_attributes[$identifier]))
  119. {
  120. return $this->_attributes[$identifier];
  121. }
  122.  
  123. return false;
  124. }
  125. }
  126.  
  127. /**
  128.   * Magic method
  129.   *
  130.   * @return string
  131.   */
  132. public function __toString()
  133. {
  134. return $this->getHtml();
  135. }
  136. }


Voorbeeld
  1. // Inladen van de class
  2. require_once 'Table.class.php';
  3.  
  4. // Opmaken van een eenvoudige tabel met enkele attributen voor de table en één kolom
  5. $table = New Html_Table;
  6. $table->attr('width', '100%')->attr('style', 'border: 1px solid red;')->attr('id', 'overview-table');
  7. $table->addRow('head')->addColumn('Title')->addColumn('Author')->addColumn('Views');
  8. $table->addRow('body')->addColumn('Leven in het wild')->addColumn('Gerard Klomp', array('style' => 'color: red;'))->addColumn('10000');
  9. $table->addRow('body')->addColumn('Leven in het licht')->addColumn('Koen van den Wijngaert')->addColumn('10000');
  10. $table->addRow('foot')->addColumn('')->addColumn('')->addColumn('20000');
  11.  
  12. // Toevoegen van een body rij met enkele kolommen, maar nu met de objecten
  13. $darkRow = New Html_Table_Row('body');
  14.  
  15. $titleColumn = New Html_Table_Column('Leven in het donker');
  16. $authorColumn = New Html_Table_Column('Ralph Thielen');
  17. $viewsColumn = New Html_Table_Column('0');
  18.  
  19. $darkRow->addColumn($titleColumn)->addColumn($authorColumn)->addColumn($viewsColumn);
  20.  
  21. $table->addRow($darkRow);
  22.  
  23. // Tonen van de tabel met tabs voor indenting.
  24. echo $table->getHtml();
  25.  
  26. // Tonen van de tabel met 4 spaties voor inspringing
  27. echo $table->getHtml(true, 4);
  28.  
  29. // Tonen van de tabel zonder inspringing
  30. echo $table->getHtml();
  31.  
  32. // Tonen van de tabel met gebruik van de magic __toString() methode, is gelijk aan $table->getHtml()
  33. echo $table;
Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (3)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.084s