Return to Snippet

Revision: 9863
at November 26, 2008 12:35 by section31


Initial Code
// Example Usage: array_to_tablerows(array('x', 'x', 'x', 'x', 'x', 'x'), 4);
// Returns: <tr><td>X</td><td>X</td><td>X</td><td>X</td></tr><tr><td>X</td><td>X</td><td colspan="2">&nbsp;</td></tr>
// Table with 4 columns with 6 values from left to right.

function array_to_tablerows($array = array(), $columns = 2) {
	if (!is_array($array) || $columns < 1) return false;
	$table = '<tr>';
	$i = 1;
	$array_length = count($array);
	foreach ($array as $v) {
		$remainder = $i % $columns;
		$table .= "<td>$v</td>";
		if ($i == $array_length) {
			$table .= ($remainder ? ('<td colspan="' . abs($columns - $remainder) . '">&nbsp;</td>') : '') . '</tr>';
		}
		elseif ($remainder == 0) {
			$table .= '</tr><tr>';
		}
		++$i;
	}
	return $table;
}

Initial URL

                                

Initial Description

                                

Initial Title
Array To Tablerows

Initial Tags

                                

Initial Language
PHP