Return to Snippet

Revision: 16613
at August 9, 2009 16:32 by kirik


Initial Code
<?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
    {
        $tr = "\n".str_repeat("\t", $tabs);
        $td = $tr."\t";
    }

    if($type == 1)
    {
        $all_columns = array_chunk($data, ceil(count($data) / $columns));
        for($i = 0, $c = count($all_columns[0]); $i < $c; $i++)
        {
            $tbl .= $tr.'<tr>';

            for($si = 0; $si < $columns; $si++)
            {
                $tbl .= $td.'<td>'.(isset($all_columns[$si][$i]) ? $all_columns[$si][$i] : '&nbsp;').'</td>';
            }

            $tbl .= $tr.'</tr>';
        }
    }
    else
    {
        for($i = 0, $n = 1, $d = ceil(count($data) / $columns) * $columns; $i < $d; $i++, $n++)
        {
            if($n == 1)
                $tbl .= $tr.'<tr>';

            $tbl .= $td.'<td>'.(isset($data[$i]) ? $data[$i] : '&nbsp;').'</td>';

            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>&nbsp;</td>
	<td>&nbsp;</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>&nbsp;</td></tr><tr><td>3</td><td>6</td><td>&nbsp;</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>';
**/

Initial URL


Initial Description
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 (:

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

Initial Tags
table, data, html

Initial Language
PHP