I'm sure you want to do more than just display the information with whatever the keys might be, and as they change from time to time, but this key-adaptive approach works for me.
Note that the tables are just 2D arrays. The
foreach ($table as $row; mimics the
$row = mysql_fetch_array($result);
Click "Toggle Plain Text" to see the lines unwrapped.
<?php
$table1 = array (
array ("Field1"=>"Value11", "Field2"=>"Value12", "Field3"=>"Value13"),
array ("Field1"=>"Value21", "Field2"=>"Value22", "Field3"=>"Value23"),
array ("Field1"=>"Value31", "Field2"=>"Value32", "Field3"=>"Value33"));
$table2 = array (
array ("NewName1"=>"NewValue11", "NewName2"=>"NewValue12", "NewName3"=>"NewValue13"),
array ("NewName1"=>"NewValue21", "NewName2"=>"NewValue22", "NewName3"=>"NewValue23"),
array ("NewName1"=>"NewValue31", "NewName2"=>"NewValue32", "NewName3"=>"NewValue33"));
function showTable ($table)
{
$columnHeads = array_keys ($table[0]);
echo "<pre>";
foreach ($columnHeads as $head)
echo $head."\t\t";
echo "<br>";
foreach ($columnHeads as $head)
{
for ($i = 0; $i < strlen ($head); $i++)
echo "-";
echo "\t\t";
}
echo "<br>";
foreach ($table as $row)
{
foreach ($columnHeads as $head) echo $row [$head]."\t\t";
echo "<br>";
}
echo "<br>";
echo "</pre>";
}
showTable ($table1);
showTable ($table2);
?>
Output:
Field1 Field2 Field3
------ ------ ------
Value11 Value12 Value13
Value21 Value22 Value23
Value31 Value32 Value33
NewName1 NewName2 NewName3
-------- -------- --------
NewValue11 NewValue12 NewValue13
NewValue21 NewValue22 NewValue23
NewValue31 NewValue32 NewValue33