PDO Database Auto Table Creator How can I do the following with less nested loops.
How can I output the Table names above their corresponding tables without having to loop.

Functions for returning the DB Info:

// Return all the Table names in an array
public function getDbTables() {          
    $result = $this-&gt;query("SHOW TABLES");
    while ($row = $result-&gt;fetchAll(PDO::FETCH_COLUMN)) {
        return $row;
    }       
}

// Return ALL column names for each Table available
public function getColumnNames($table){
    $sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = :table";
    try {
        $stmt = $this-&gt;dbh-&gt;prepare($sql);
        $stmt-&gt;bindValue(':table', $table, PDO::PARAM_STR);
        $stmt-&gt;execute();
        $output = array();
        while($row = $stmt-&gt;fetch(PDO::FETCH_ASSOC)){
            $output[] = $row['COLUMN_NAME'];                
        }
        return $output; 
    }

    catch(PDOException $pe) {
        trigger_error('Could not connect to MySQL database. ' . $pe-&gt;getMessage() , E_USER_ERROR);
    }
}

// Populate HTML Table with the DB Table Data
public function populateTable($table) {          
    $sql = "SELECT * FROM $table";
    try {
        $stmt = $this-&gt;dbh-&gt;prepare($sql);
        $stmt-&gt;execute();
        while($row = $stmt-&gt;fetch(PDO::FETCH_ASSOC)){
            return $row;              
        }

    }
        catch(PDOException $pe) {
        trigger_error('Could not connect to MySQL database. ' . $pe-&gt;getMessage() , E_USER_ERROR);
    }
}


HTML Output:

&lt;?php print_r($phpCore-&gt;getDbTables()); ?&gt;
&lt;div&gt;
&lt;form&gt;
&lt;table&gt;
&lt;?php
foreach($phpCore-&gt;getDbTables() as $tablename) {
    echo '&lt;tr&gt;';
    foreach($phpCore-&gt;getColumnNames($tablename) as $fieldnames) {
        echo '&lt;td&gt;'.$fieldnames.'&lt;/td&gt;';    
    }
    echo '&lt;/tr&gt;';

    echo '&lt;tr&gt;';
    foreach($phpCore-&gt;populateTable($tablename) as $tabledata) {
        echo '&lt;td&gt;&lt;input type="text" value="'.$tabledata.'"&gt;&lt;/input&gt;&lt;/td&gt;';
    }   
    echo '&lt;/tr&gt;';
}
?&gt;
&lt;/table&gt;
&lt;/form&gt;
&lt;/div&gt;


Visual Output:
Ok, so I'm happy with the foreach loops now.

To get the Table names where I wanted them I simply had to encapsulate the PHP in HTML tags:

&lt;?php print_r($phpCore-&gt;getDbTables()); ?&gt;
&lt;div&gt;
&lt;form&gt;
&lt;table&gt;
&lt;?php
foreach($phpCore-&gt;getDbTables() as $tablename) {
    echo '&lt;tr&gt;&lt;td&gt;'.$tablename.'&lt;/td&gt;&lt;/tr&gt;';
    echo '&lt;tr&gt;';
    foreach($phpCore-&gt;getColumnNames($tablename) as $fieldnames) {
        echo '&lt;td&gt;'.$fieldnames.'&lt;/td&gt;';    
    }
    echo '&lt;/tr&gt;';

    echo '&lt;tr&gt;';
    foreach($phpCore-&gt;populateTable($tablename) as $tabledata) {
        echo '&lt;td&gt;&lt;input type="text" value="'.$tabledata.'"&gt;&lt;/input&gt;&lt;/td&gt;';
    }   
    echo '&lt;/tr&gt;';

}
?&gt;
&lt;/table&gt;
&lt;/form&gt;
&lt;/div&gt;