HTML table output using Nested for loops in PHP
By: Anonymous Printer Friendly Format
Nest for loops are used for handling many difficult situations such as Matrix, Recursion etc..In this example, it is shown how nested for loops are useful for outputting a data array into a table (ie. images). Additionally in this example, the Nested For Loop uses the same iterator as the parent for loop. Well formatted so the resulting code is clean when executed.
<?php
//Dummy data
$data =
array(73,74,75,76,78,79,80,81,82,83,84,85,86,87);
//Our 'stepping' variable
$g = 0;
//Our rowcount
$rowcount = 0;
echo "<table cellspacing='0'>\r";
for ($i=0;
$i<count($data); ) {
$rowcount++;
echo
" <tr>\r"; //New row
$g = $i + 3; //Set our nested
limit
for( ; $i<$g; $i++) {
//nested for loop
if
(!isset($data[$i])) { //Allow us to break on incomplete
rows
break;
}
echo
" <td style='border: 1px #000
solid;'>\r"; //Out put a
cell
echo
" <p>Row
$rowcount <br/> Cell: $i <br/> Data:
$data[$i]</p>\r";
echo "
</td>\r";
}
echo "
</tr> \r"; //End New Row
}
echo "</table>\r";?>
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
PHP code to import from CSV file to MySQL
PHP code to write to a CSV file from MySQL query
PHP code to write to a CSV file for Microsoft Applications
Password must include both numeric and alphabetic characters - Magento
PHP file upload prompts authentication for anonymous users
PHP file upload with IIS on windows XP/2000 etc
Error: Length parameter must be greater than 0
Multiple File Upload in PHP using IFRAME
Resume or Pause File Uploads in PHP
Exception in module wampmanager.exe at 000F15A0 in Windows 8