/ Published in: PHP

Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// 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. $table = '<tr>'; $i = 1; 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; }
Comments
