Posted By

kirik on 08/09/09


Tagged

table data html to


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

irishsk
umang_nine


Function for putting data to the html table [two different ways]


 / Published in: PHP
 

This function is very helpfull when you need to put some data (that was gotten from database) to HTML table. You can choose vertical or horizontal types.

Horizontal type:

| 1 | 2 | 3 |

| 4 | 5 | 6 |

| 7 | 8 | 9 |

Vertical type:

| 1 | 4 | 7 |

| 2 | 5 | 8 |

| 3 | 6 | 9 |

  • hope you will sort it out (:
  1. <?php
  2. /**
  3.  * @var $data (array)
  4.  * array to put to the html table
  5.  * @var $type (int)
  6.  * type of table (0 - horizontal, 1 - vertical)
  7.  * @var $columns (int)
  8.  * numer of columns
  9.  * @var $tabs (int)
  10.  * numer of tabs to decorate your html code. If you don't need - set this variable to false
  11. **/
  12. function drawTable($data, $type=1, $columns=10, $tabs=0)
  13. {
  14. $tbl = null;
  15.  
  16. if($tabs === false)
  17. {
  18. $tr = $td = null;
  19. }
  20. else
  21. {
  22. $tr = "\n".str_repeat("\t", $tabs);
  23. $td = $tr."\t";
  24. }
  25.  
  26. if($type == 1)
  27. {
  28. $all_columns = array_chunk($data, ceil(count($data) / $columns));
  29. for($i = 0, $c = count($all_columns[0]); $i < $c; $i++)
  30. {
  31. $tbl .= $tr.'<tr>';
  32.  
  33. for($si = 0; $si < $columns; $si++)
  34. {
  35. $tbl .= $td.'<td>'.(isset($all_columns[$si][$i]) ? $all_columns[$si][$i] : '&nbsp;').'</td>';
  36. }
  37.  
  38. $tbl .= $tr.'</tr>';
  39. }
  40. }
  41. else
  42. {
  43. for($i = 0, $n = 1, $d = ceil(count($data) / $columns) * $columns; $i < $d; $i++, $n++)
  44. {
  45. if($n == 1)
  46. $tbl .= $tr.'<tr>';
  47.  
  48. $tbl .= $td.'<td>'.(isset($data[$i]) ? $data[$i] : '&nbsp;').'</td>';
  49.  
  50. if($n == $columns)
  51. {
  52. $n = 0;
  53. $tbl .= $tr.'</tr>';
  54. }
  55. }
  56. }
  57.  
  58. if($tabs !== false)
  59. $tbl .= "\n";
  60.  
  61. return $tbl;
  62. }
  63.  
  64. /**
  65. === Example 1: numeric, horizontal, 3 columns, with 0 tabs ===
  66. $data = range(1, 7);
  67. echo '<table>'.drawTable($data, 0, 3, 0).'</table>';
  68.  
  69. --- Result 1 ---
  70. | 1 | 2 | 3 |
  71. | 4 | 5 | 6 |
  72. | 7 | | |
  73.  
  74. --- Result in HTML 1 ---
  75. <table>
  76. <tr>
  77. <td>1</td>
  78. <td>2</td>
  79. <td>3</td>
  80. </tr>
  81. <tr>
  82. <td>4</td>
  83. <td>5</td>
  84.  
  85. <td>6</td>
  86. </tr>
  87. <tr>
  88. <td>7</td>
  89. <td>&nbsp;</td>
  90. <td>&nbsp;</td>
  91. </tr>
  92. </table>
  93. **/
  94.  
  95.  
  96. /**
  97. === Example 2: numeric, vertical, 3 columns, in one line ===
  98. $data = range(1, 7);
  99. echo '<table>'.drawTable($data, 1, 3, false).'</table>';
  100.  
  101. --- Result 2 ---
  102. | 1 | 4 | 7 |
  103. | 2 | 5 | |
  104. | 3 | 6 | |
  105.  
  106. --- Result 2 in HTML ---
  107. <table><tr><td>1</td><td>4</td><td>7</td></tr><tr><td>2</td><td>5</td><td>&nbsp;</td></tr><tr><td>3</td><td>6</td><td>&nbsp;</td></tr></table>
  108. **/
  109.  
  110.  
  111. /**
  112. === Example 3: data from MySQL, horizontal, 5 columns, in one line ===
  113. $query = mysql_query('SELECT `column1`, `column2`, `column3`, `column4`, `column5` FROM `table` ORDER BY `field` DESC');
  114. while($row = mysql_fetch_row($query))
  115. {
  116.   $data[] = $row;
  117. }
  118. mysql_free_result($query);
  119. echo '<table>'.drawTable($data, 0, 5, false).'</table>';
  120. **/
  121.  
  122.  
  123. /**
  124. === Example 4: data from MySQL, vertical, 5 columns, in one line ===
  125. $query = mysql_query('SELECT `column` FROM `table` ORDER BY `field` DESC');
  126. while($row = mysql_fetch_row($query))
  127. {
  128.   $data[] = $row;
  129. }
  130. mysql_free_result($query);
  131. echo '<table>'.drawTable($data, 1, 5, false).'</table>';
  132. **/

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: adgower on August 9, 2009

tables aren't good formatting methods ;)

Posted By: MMDeveloper on August 10, 2009

it's a neat concept and I'm sure lots of people will find it useful. Tables are meant for tabular data and the OP's example is displaying.. well you guessed it.. tabular data.

Posted By: kirik on August 13, 2009

2adgower When you need to draw a table it is not good to draw it with DIV's (:

You need to login to post a comment.