HTML - Automatic generation of code for tables


SUBMITTED BY: efbee

DATE: Oct. 3, 2016, 7:43 p.m.

FORMAT: HTML

SIZE: 6.3 kB

HITS: 1169

  1. Automatic generation of HTML code for a table. OO interface
  2. Can define colspan, rowspan, table style, cell style, and data style
  3. <html>
  4. <head>
  5. <title>Table</title>
  6. <style type="text/css">
  7. .header { background: yellow; text-align: center; }
  8. </style>
  9. </head>
  10. <body>
  11. <?
  12. class html_table {
  13. var $cols = 0;
  14. var $rows = 0;
  15. var $cell = array();
  16. var $html = '';
  17. var $cell_content = ' '; //default cell content.
  18. var $content_style = ''; //default content style. content is surrounded by <SPAN> tag.
  19. var $cell_style = ''; //default cell style
  20. 0 var $table_style = ''; //default table style.
  21. function cell_init() {
  22. return array(
  23. 'content'=>$this->cell_content, // data to be inserted between <TD> tags.
  24. 'style'=>$this->content_style, // if this is defined and not empty then "content" (defined above)
  25. // is encapsulated by a <SPAN STYLE='style'> tag.
  26. 'cell_style'=>$this->cell_style); // if this is defined and not empty then the <TD> tag that encapsulates
  27. // "content" has its "$cell_style" replaced by this variable.
  28. }
  29. function init($parameters) {
  30. if (isset($parameters['cols'])) $this->cols = $parameters['cols'];
  31. if (isset($parameters['rows']))$this->rows = $parameters['rows'];
  32. for ($row = 1; $row <= $this->rows; $row++) {
  33. for ($col = 1; $col <= $this->cols; $col++) {
  34. $this->cell[$row][$col] = $this->cell_init();
  35. }
  36. }
  37. }
  38. function add_rows($num_rows) {
  39. for ($row = 1; $row <= $num_rows; $row++) {
  40. for ($col = 1; $col <= $this->cols; $col++) {
  41. $this->cell[$row + $this->rows][$col] = $this->cell_init();
  42. }
  43. }
  44. $this->rows += $num_rows;
  45. }
  46. function add_cols($num_cols) {
  47. for ($row = 1; $row <= $this->rows; $row++) {
  48. for ($col = 1; $col <= $num_cols; $col++) {
  49. $this->cell[$row][$col+$this->cols] = $this->cell_init();
  50. }
  51. }
  52. $this->cols += $num_cols;
  53. }
  54. function code() {
  55. if (!empty($this->html)) return 1;
  56. $this->html = '<TABLE '.$this->table_style.'>'."\n";
  57. for ($row = 1; $row <= $this->rows; $row++) {
  58. $this->html .= '<TR>'."\n";
  59. for ($col = 1; $col <= $this->cols; $col++) {
  60. $extra = '';
  61. //check if "colspan" defined. if so then hide cells that get merged.
  62. if (isset($this->cell[$row][$col]['colspan'])) {
  63. $extra .= 'COLSPAN="'.$this->cell[$row][$col]['colspan'].'"';
  64. for ($hidden_col = 1; $hidden_col < $this->cell[$row][$col]['colspan']; $hidden_col++) {
  65. $this->cell[$row][$col+$hidden_col]["hide"] = true;
  66. //check if "rowspan" defined. if so then propogate "colspan" into merged rows.
  67. if (isset($this->cell[$row][$col]["rowspan"])) {
  68. for ($hidden_row = 1; $hidden_row < $this->cell[$row][$col]['rowspan']; $hidden_row++) {
  69. $this->cell[$row+$hidden_row][$col]["colspan"] = $this->cell[$row][$col]['colspan'];
  70. }
  71. }
  72. }
  73. }
  74. //check if "rowspan" defined. if so then hide cells that get merged.
  75. if (isset($this->cell[$row][$col]["rowspan"])) {
  76. $extra .= 'ROWSPAN="'.$this->cell[$row][$col]['rowspan'].'"';
  77. for ($hidden_row = 1; $hidden_row < $this->cell[$row][$col]['rowspan']; $hidden_row++) {
  78. $this->cell[$row+$hidden_row][$col]["hide"] = true;
  79. }
  80. }
  81. // code to draw cell html...
  82. if (isset($this->cell[$row][$col]['hide'])) continue; // if hide then skip this cell.
  83. // otherwise draw cell with style...
  84. if (!empty($this->cell[$row][$col]['cell_style']))
  85. $this->html .= '<TD '.$this->cell[$row][$col]['cell_style'].' '.$extra.'>';
  86. else
  87. $this->html .= '<TD '.$this->cell_style.' '.$extra.'>';
  88. // draw content of cell with style...
  89. if (!empty($this->cell[$row][$col]['style'])) $this->html .= '<SPAN STYLE="'.$this->cell[$row][$col]['style'].'">';
  90. else if (!empty($this->content_style)) $this->html .= '<SPAN STYLE="'.$this->content_style.'">';
  91. $this->html .= $this->cell[$row][$col]['content'];
  92. if (!empty($this->cell[$row][$col]['style']) or !empty($this->content_style)) $this->html .= '</SPAN>';
  93. $this->html .= '</TD>'."\n";
  94. }
  95. $this->html .= '</TR>'."\n";
  96. }
  97. $this->html .= '</TABLE>'."\n";
  98. }
  99. function display() {
  100. print $this->html;
  101. }
  102. }
  103. /*
  104. example below...
  105. */
  106. $data = new html_table;
  107. // define table size.
  108. $data->init(array("cols"=>3, "rows"=>"3"));
  109. // default styles.
  110. $data->table_style = 'BORDER="1" CELLPADDING="2" CELLSPACING="2"';
  111. $data->cell_style = 'WIDTH="100" ALIGN="center" BGCOLOR="white"';
  112. $data->content_style = 'font: bold 9pt monospace;';
  113. // define table content.
  114. $data->cell[1][1]["content"]="apple";
  115. $data->cell[2][2]["content"]="orange";
  116. $data->cell[2][3]["content"]="pear";
  117. $data->cell[3][1]["content"]="plum";
  118. $data->cell[2][1]["content"]="grape";
  119. $data->cell[1][1]["colspan"]=3;
  120. $data->cell[2][3]["rowspan"]=2;
  121. $data->cell[3][1]["colspan"]=2;
  122. // define fancy styles for individual cells.
  123. $data->cell[2][2]["style"] ='font: 18pt monospace;';
  124. $data->cell[2][3]["style"] ='background: red';
  125. $data->cell[1][1]["style"] ='color: red; font: italic 18pt sans-serif;';
  126. $data->cell[3][1]["style"] ='color: white; font: bold 18pt serif;';
  127. $data->cell[1][1]["cell_style"] = 'CLASS="header"';
  128. $data->cell[3][1]["cell_style"] = 'ALIGN="right" bgcolor="blue"';
  129. // code and display.
  130. $data->code();
  131. $data->display();
  132. ?>
  133. </body>
  134. </html>

comments powered by Disqus