Archive

Archive for April, 2013

Show data in multicolumns

April 18, 2013 1 comment

Sometimes we need to show data in multi columns. Here is the little script for this logic.
First we need data in array. Let’s create a array.
Sometimes we need to show data in multi columns. Here is the little script for this logic.
First we need data in array. Let’s create an array and display this data in two column.


$columns =2;//No of column
$dataToDisplay = array('green', 'blue', 'white', 'apple', 'computers', 'php');
echo "<table width='100%'>";
$columnwidth = 100/$columns;
$count = count($dataToDisplay);
$i =0;
foreach($dataToDisplay as $data) {
    if($i % $columns == 0){
        echo "<tr>";
    }
    echo "<td>" . $data . "</td>";
    if(($i % $columns) == ($columns - 1) || ($i + 1) == $columns){
        echo "</tr>";
    }
    $i++;
}
echo "</table>";
Advertisements
%d bloggers like this: