<

Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x

Dynamically generating tables in PHP

Published on
10,868 Points
3,868 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Join & Write a Comment

Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month