使用php的mysql记录动态生成html表

The reason this is complicated (for me) is that each column of the table is loaded from a separate MySQL table, and each MySQL table will have varying number of records. Initially I thought I could start generating the html table from top-left to bottom-right column by column , cell by cell, but this won't work because each MySQL table will have different length of records, which generate malformed html tables. Do you have any suggestions?

My idea so far:

  • Get a list of all tables in MySQL, which determine the number of columns
  • Get the count from the table with most records
  • Create a table with the parameters (# of tables as columns, max# as rows
  • Update each cell with the corresponding record, but not quite sure how

As requested, some code:

$tables = mysql_query("show tables");

$output = "<table border=1><thead><tr>";

while($table = mysql_fetch_array($tables)) {

$output .= "<td>";
$output .= $table[0];
$output .= "</td>";
$tableNames[] = $table[0];

}

$output .= "</tr></thead>";
$output .= "<tbody>";

//Get a count of the table with the most records
for($i=0; $i<count($tableNames); $i++ ){

    $currentTable = $tableNames[$i];

    $tableContent = mysql_query("select * from $currentTable") or die("Error: ".mysql_error());

    //Generating all content for a column
    $output .= "<tr>";

    while($content = mysql_fetch_array($tableContent)){

        //generating a cell in the column
        $output .= "<td>";
        $output .= "<strong>".$content['subtheme'].": </strong>";
        $output .= $content['content'];
        $output .= "</td>";

    }

    $output .= "</tr>";

}

$output .= "</tbody>";
$output .= "</table>";

This is wrong not just because it generates a malformed table, but also because it transposed columns to rows...

Any help would be appreciated

Solution to my much hated question:

$mymax = 0;
for($i=0; $i<count($tableNames); $i++){
    $currentTable = $tableNames[$i];
    $tableCounts = "select * from $currentTable";

    if($stmt = $mysqli->prepare($tableCounts)){

        mysqli_stmt_execute($stmt);
        mysqli_stmt_store_result($stmt);
        $count = mysqli_stmt_num_rows($stmt);
        mysqli_stmt_close($stmt);

    }   
    ($mymax >= $count ? "" : $mymax = $count);

    $colWidth = 100 / count($tableNames);   
}

// DIV GRID
// via DIV GENERATION
$output .= "<div class='grid'>";
for ($i=0; $i<count($tableNames); $i++){
    $output .= "<div id='col$i' class='col' style=\"width:$colWidth%\">";
    $output .= "<h3>".$tableNames[$i]."</h3>";

    $tableqry = "select * from $tableNames[$i]";

    if ($result = mysqli_query($mysqli, $tableqry)) {

        while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){

            $output .= "<div class='item'>".$row["content"]."</div>";

        }

        mysqli_free_result($result);

    }

    $output .= "</div>";

}

$output .="</div>";

$output .="<div class='clear'></div>";