Home » HTML, PHP

Creating dynamic table in PHP : Easy and Simple tutorial

9 December 2007 1,919 views Popularity: 4% Share/Bookmark

email

Problem:

I want to create a dynamic table with a row starting after certain number of columns, i.e. like, i want to create an html table which automatically generate new row after 3 columns in a row.

Solution:

$content = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
<table border="1" width="50%">
<tr>
	<?php
	$counter = 0;
	foreach($content as $data) {
		if($counter != 0 && $counter%3 == 0) {
			?>
			</tr><tr>
			<?php
		}
		?>
		<td>
			<?php echo $data; ?>
		</td>
		<?php
		$counter++;
	}
?>
</tr>
</table>

Here, $content is an array of 9 numbers (1 to 9). The array value can be any. You can have your custom array with your custom values. I have looped through the array and printed data in a html table. I have created new row after 3 columns have been created.

Happy PHPing!

Related posts:

  1. Add, edit, delete, login, register in PHP :: A simple and complete tutorial
  2. PHP: How to get stock quote data from Yahoo! Finance? (Complete Code and Tutorial)
  3. Alter MySQL table to add & drop column & add Foreign Key
  4. Simple and easy jQuery tabs with AJAX and PHP
  5. Very simple add, edit, delete, display in PHP
  6. PHP: Simple and easy way to format URL string
  7. How to get(view) html source code of a website
  8. Inheritance in PHP :: A Simple Example
  9. jQuery: Grey out background and preview image as popup
  10. Creating selection list, using foreach and section loop in Smarty
  • Yrollgayanth

    Thanks a lot, this is very useful