1fd195404fcc209f9d9594d92b33de0a

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?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">&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;
}


?>

Refactorings

No refactoring yet !

5a00a3a98dcf6f9cd717440fd2b606e5

Eineki

October 25, 2008, October 25, 2008 07:00, permalink

No rating. Login to rate!

Hi, try this version, it use the array_slice predefined function to simplify the code.

Edit: overlooked the <td colspan="2">&nbsp;</td> part. Fixed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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">&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='';
    $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 .'">&nbsp;</td></tr>';
    }
    return $table;
}

?>
B066cb3c505933f832faa83238489a89

halogenandtoast

October 26, 2008, October 26, 2008 21:03, permalink

No rating. Login to rate!

Here is the shortest I could get your code, it uses recursion, but there is a lot less arithmetic

1
2
3
4
5
6
7
8
9
10
11
<?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).'">&nbsp;</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);
}
?>
B066cb3c505933f832faa83238489a89

halogenandtoast

October 26, 2008, October 26, 2008 22:41, permalink

2 ratings. Login to rate!

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.

1
2
3
4
5
6
<?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))."\">&nbsp;</td>");'), array_chunk($ary, $max)))."</tr>";
}

?>
5a00a3a98dcf6f9cd717440fd2b606e5

Eineki

October 26, 2008, October 26, 2008 22:57, permalink

No rating. Login to rate!

My previous version shortened is only 4 lines. I prefer the other one though

1
2
3
4
5
6
7
<?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)).'">&nbsp</td></tr>' : $tbl;
}
5a00a3a98dcf6f9cd717440fd2b606e5

Eineki

October 26, 2008, October 26, 2008 23:00, permalink

No rating. Login to rate!

cool version the one liner one. missed the fifth star just for a mouse glitch

151e36cc7f789a4790c8ca437e3a1f60

Chris Dean

October 28, 2008, October 28, 2008 07:57, permalink

No rating. Login to rate!

Love the one liner, but at the same time omg.

Your refactoring





Format Copy from initial code

or Cancel