这很复杂(对我来说)的原因是表的每一列都是从一个单独的MySQL表加载的,每个MySQL表都有不同数量的记录。最初我以为我可以逐个列地逐个列地从左上角到右下角开始生成html表,但这不起作用,因为每个MySQL表都有不同的记录长度,这会生成格式错误的html表。你有什么建议吗?
到目前为止我的想法:
根据要求,一些代码:
$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>";
这是错误的,不仅因为它生成了格式错误的表,还因为它将列转换为行...
任何帮助将不胜感激
答案 0 :(得分:1)
解决我讨厌的问题:
$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>";