Programming Tutorials

HTML table output using Nested for loops in PHP

By: Anonymous in PHP Tutorials on 2012-04-05  

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";
?>





Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in PHP )

PHP code to write to a CSV file from MySQL query

Different versions of PHP - History and evolution of PHP

PHP code to import from CSV file to MySQL

Encrypting files using GnuPG (GPG) via PHP

PHP Warning: Unknown(): Unable to load dynamic library '/usr/local/php4/lib/php/extensions/no-debug ......

Decrypting files using GnuPG (GPG) via PHP

Send push notifications using Expo tokens in PHP

A Basic Example using PHP in AWS (Amazon Web Services)

Count occurrences of a character in a String in PHP

Password must include both numeric and alphabetic characters - Magento

Error: Length parameter must be greater than 0

Reading word by word from a file in PHP

Parent: child process exited with status 3221225477 -- Restarting

Convert a hex string into a 32-bit IEEE 754 float number in PHP

PHP file upload prompts authentication for anonymous users

Latest Articles (in PHP)