/ Published in: PHP
Let's say you want to show a list of employees, and you want to show 3 on each row, use the script below to accomplish that.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php // My array with employees // sample purpose only for( $i = 0; $i < 10; $i++ ) { $arr_array[$i] = $i+1; } $columns = 3; // employees pr. row $i = 0; $j = 1; $output.= '<table cellpadding="50" cellspacing="10" border="1"> <tr>'; foreach( $arr_array as $key => $value ) { if ( $i >= $columns ) { $output .= '</tr><tr>'; $i = 0; } if ( $j <= $columns ) { $class = 'first'; } else if ( (($amount+$amount_td)-$j) < $columns ) { $class = 'last'; } else { $class = ''; } $output.= '<td class="' . $class . '">' . $value . '</td>'; $i++; $j++; } for( $i = 0; $i < $amount_td; $i++ ) { $output.= '<td class="last"> </td>'; } $output .= '</tr> </table>'; ?> <!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title>Table</title> </head> <body> <?php print $output; ?> </body> </html>