/ 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 (:
                **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 (:
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
<?php
/**
* @var $data (array)
* array to put to the html table
* @var $type (int)
* type of table (0 - horizontal, 1 - vertical)
* @var $columns (int)
* numer of columns
* @var $tabs (int)
* numer of tabs to decorate your html code. If you don't need - set this variable to false
**/
function drawTable($data, $type=1, $columns=10, $tabs=0)
{
$tbl = null;
if($tabs === false)
{
$tr = $td = null;
}
else
{
$td = $tr."\t";
}
if($type == 1)
{
{
$tbl .= $tr.'<tr>';
for($si = 0; $si < $columns; $si++)
{
}
$tbl .= $tr.'</tr>';
}
}
else
{
{
if($n == 1)
$tbl .= $tr.'<tr>';
if($n == $columns)
{
$n = 0;
$tbl .= $tr.'</tr>';
}
}
}
if($tabs !== false)
$tbl .= "\n";
return $tbl;
}
/**
=== Example 1: numeric, horizontal, 3 columns, with 0 tabs ===
$data = range(1, 7);
echo '<table>'.drawTable($data, 0, 3, 0).'</table>';
--- Result 1 ---
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | | |
--- Result in HTML 1 ---
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td> </td>
<td> </td>
</tr>
</table>
**/
/**
=== Example 2: numeric, vertical, 3 columns, in one line ===
$data = range(1, 7);
echo '<table>'.drawTable($data, 1, 3, false).'</table>';
--- Result 2 ---
| 1 | 4 | 7 |
| 2 | 5 | |
| 3 | 6 | |
--- Result 2 in HTML ---
<table><tr><td>1</td><td>4</td><td>7</td></tr><tr><td>2</td><td>5</td><td> </td></tr><tr><td>3</td><td>6</td><td> </td></tr></table>
**/
/**
=== Example 3: data from MySQL, horizontal, 5 columns, in one line ===
$query = mysql_query('SELECT `column1`, `column2`, `column3`, `column4`, `column5` FROM `table` ORDER BY `field` DESC');
while($row = mysql_fetch_row($query))
{
$data[] = $row;
}
mysql_free_result($query);
echo '<table>'.drawTable($data, 0, 5, false).'</table>';
**/
/**
=== Example 4: data from MySQL, vertical, 5 columns, in one line ===
$query = mysql_query('SELECT `column` FROM `table` ORDER BY `field` DESC');
while($row = mysql_fetch_row($query))
{
$data[] = $row;
}
mysql_free_result($query);
echo '<table>'.drawTable($data, 1, 5, false).'</table>';
**/
Comments
 Subscribe to comments
                    Subscribe to comments
                
                