<?php
// 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"> </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) . '"> </td>') : '') . '</tr>';
}
elseif ($remainder == 0) {
$table .= '</tr><tr>';
}
++$i;
}
return $table;
}
?>
Refactorings
No refactoring yet !
Eineki
October 25, 2008, October 25, 2008 07:00, permalink
Hi, try this version, it use the array_slice predefined function to simplify the code.
Edit: overlooked the <td colspan="2"> </td> part. Fixed.
<?php
// 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"> </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='';
$arrayLen = count($array);
for ($i=0; $i<$arrayLen; $i+=$columns) {
$table .= '<tr><td>'. implode('</td><td>',array_slice($array, $i, $columns)) . '</td></tr>';
}
$colspan = $columns - ($arrayLen % $columns);
if ($colspan != $columns) {
$table = substr($table,0,strlen($table)-5).'<td colspan="'. $colspan .'"> </td></tr>';
}
return $table;
}
?>
halogenandtoast
October 26, 2008, October 26, 2008 21:03, permalink
Here is the shortest I could get your code, it uses recursion, but there is a lot less arithmetic
<?php
function array_to_tablerows($ary, $max) { return array_create_table($ary, 0, $max); }
function array_create_table($ary, $idx, $max) {
if(empty($ary)) {
return $idx ? '<td colspan="'.($max-$idx).'"> </td></tr>' : '</tr>';
} elseif(!$idx) $out.= '<tr>';
$out .= "<td>".array_shift($ary)."</td>";
if(!($idx = ($idx + 1) % $max) && !empty($ary)) $out.= '</tr>';
return $out . array_create_table($ary, $idx, $max);
}
?>
halogenandtoast
October 26, 2008, October 26, 2008 22:41, permalink
Okay had to try and make a one-liner out of this, probably not as useful as my last solution but interesting none-the-less.
<?php
function array_to_tablerows($ary, $max) {
return empty($ary) ? null : "<tr>".implode("</tr><tr>", array_map(create_function('$a', 'return "<td>".implode("</td><td>", $a)."</td>".((count($a) % '.$max.' == 0) ? null : "<td colspan=\"".('.$max.' - count($a))."\"> </td>");'), array_chunk($ary, $max)))."</tr>";
}
?>
Eineki
October 26, 2008, October 26, 2008 22:57, permalink
My previous version shortened is only 4 lines. I prefer the other one though
<?php
function array_to_tablerows($array = array(), $cols = 2) {
if (!is_array($array) || $cols < 1) return false;
for ($i=0, $tbl='', $len = count($array), $i<$len; $i+=$cols)
$tbl .= '<tr><td>'. implode('</td><td>',array_slice($array, $i, $cols)) . '</td></tr>';
return ($len % $cols) ? substr($tbl,0,strlen($tbl)-5).'<td colspan="'.($cols-($len % $cols)).'"> </td></tr>' : $tbl;
}
Eineki
October 26, 2008, October 26, 2008 23:00, permalink
cool version the one liner one. missed the fifth star just for a mouse glitch
Chris Dean
October 28, 2008, October 28, 2008 07:57, permalink
Love the one liner, but at the same time omg.
Coverts an Array of Values to a multi-column table row.
A working demo is available at http://section31.us/study/dev/php/arrayvalues_to_table.php
Source for demo site is here http://section31.us/study/dev/php/arrayvalues_to_table.php?source