Return to Snippet

Revision: 56880
at May 8, 2012 19:52 by andreaskian


Updated Code
<?php 
// My array with employees
// sample purpose only
$arr_array  = array();

for( $i = 0; $i < 10; $i++ ) {

    $arr_array[$i] = $i+1;

}

$columns    = 3;                                                  // employees pr. row
$amount     = count($arr_array);                                  // employees total
$amount_td  = $columns * (ceil( $amount / $columns )) - $amount;  // empty rows to create
$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">&nbsp;</td>';
}

$output .= '</tr>
</table>';

?>
<!DOCTYPE html>
<html>
<head>
    <meta charset=utf-8 />
    <title>Table</title>
</head>
<body>

<?php print $output; ?>

</body>
</html>

Revision: 56879
at April 19, 2012 23:59 by andreaskian


Initial Code
<?php 

$arr_array  = array();

for( $i = 0; $i < 10; $i++ ) {

    $arr_array[$i] = $i+1;

}

$columns    = 3;
$amount     = count($arr_array);
$amount_td  = $columns*(ceil($amount/$columns))-$amount;
$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">&nbsp;</td>';
}

$output .= '</tr>
</table>';

?>
<!DOCTYPE html>
<html>
<head>
    <meta charset=utf-8 />
    <title>Table</title>
</head>
<body>

<?php print $output; ?>

</body>
</html>

Initial URL

                                

Initial Description
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.

Initial Title
Create table rows automatically based on number of columns

Initial Tags
table

Initial Language
PHP