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->query("SHOW TABLES");
while ($row = $result->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->dbh->prepare($sql);
$stmt->bindValue(':table', $table, PDO::PARAM_STR);
$stmt->execute();
$output = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$output[] = $row['COLUMN_NAME'];
}
return $output;
}
catch(PDOException $pe) {
trigger_error('Could not connect to MySQL database. ' . $pe->getMessage() , E_USER_ERROR);
}
}
// Populate HTML Table with the DB Table Data
public function populateTable($table) {
$sql = "SELECT * FROM $table";
try {
$stmt = $this->dbh->prepare($sql);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
return $row;
}
}
catch(PDOException $pe) {
trigger_error('Could not connect to MySQL database. ' . $pe->getMessage() , E_USER_ERROR);
}
}
HTML Output:
<?php print_r($phpCore->getDbTables()); ?>
<div>
<form>
<table>
<?php
foreach($phpCore->getDbTables() as $tablename) {
echo '<tr>';
foreach($phpCore->getColumnNames($tablename) as $fieldnames) {
echo '<td>'.$fieldnames.'</td>';
}
echo '</tr>';
echo '<tr>';
foreach($phpCore->populateTable($tablename) as $tabledata) {
echo '<td><input type="text" value="'.$tabledata.'"></input></td>';
}
echo '</tr>';
}
?>
</table>
</form>
</div>
Visual Output: