Home > php > Show data in multicolumns

Show data in multicolumns


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) == $num_rows){
        echo "</tr>";
    }
    $i++;
}
echo "</table>";
About these ads
  1. April 16, 2014 at 3:42 pm

    Thanks for this simple example, helped me big time!
    Although, you haven’t defined num_rows in the beginning :)

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: