<?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
?>
if($i % 3 == 0 && $i !=0 ){
echo "</tr><tr>"; // closing old row and opening new row
}
if($i % 5 == 0 && $i !=0 ){
echo "</tr><tr>"; // closing old row and opening new row
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)