<

Dynamically generating tables in PHP

Published on
10,724 Points
3,724 Views
Last Modified:
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths in the for loop.
Formatting data and displaying it in a grid format is needed most of the times. PHP developers who are good at coding also face this kind of problem due to lack of html knowledge. Also I have seen many questions at experts exchange who asks for formatting data and dynamically creating table. Most of the times developer stuck at the basic row/column problem.

Let me explain you this concept with example. So lets consider I have to generate a table where each row(horizontal) consists of 3 columns (vertical). Basic html syntax used will be <table> , <tr>  (row) , <td> (column).

Basic scenario is as follows:

Input:
$arr = array('a','b','c','d','e','f','g','h','i'); // test array containing 9 elements to be displayed in grid format

Expected output:
--------------
  a  |  b |  c |
--------------
  d  |  e |  f  |
--------------
  g  |  h |  i  |
--------------

So here is the code to dynamically generate table in php :
<?php

echo "Creating table dynamically";

$arr = array('a','b','c','d','e','f','g','h','i'); // test array containing 9 elements to be displayed in grid format

$tot_cnt = count($arr);                        // calculating the count of elements in the array i.e. 9

echo "<table border=1><tr>"; // start table and open first row

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

  // In the below if loop a check is performed to see that if 3 columns 
 // are created than close the old row and open new row

   if($i % 3 == 0 && $i  !=0 ){ 
      echo "</tr><tr>";                // closing old row and opening new row 
   }			 

  echo "<td>".$arr[$i]."</td>"; // add data in columns

}

echo "</tr></table>"; // close last row and table
?>

Open in new window


you can see in the above code that for placing 3 columns in a row this condition is important:
   if($i % 3 == 0 && $i  !=0 ){ 
      echo "</tr><tr>";                // closing old row and opening new row 
   }			 

Open in new window


so if you need to place say 5 columns in a row than do just this.....
   if($i % 5 == 0 && $i  !=0 ){ 
      echo "</tr><tr>";                // closing old row and opening new row 
   }			 

Open in new window


So basically the number with which you perform mod(%) operation in crucial. The math here is simple ... the mod(%) operator gives the remainder of the division so say for example 25 % 3 will be 1. hope you are clear with the mod concept if not I have provided a reference urls below you may checkout those.

Hope this helps to most of the php developers searching for formatting data in a table.

References for your help:

If you want to understand how html table works check out this ...

http://www.w3schools.com/html/html_tables.asp

if you want to understand how moduli operator works check out this:

http://php.net/manual/en/language.operators.arithmetic.php

-Sandeep
0
Comment
Author:kshna
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
0 Comments

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Join & Write a Comment

The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month