首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态生成带有php的mysql记录的html表

问动态生成带有php的mysql记录的html表
EN

Stack Overflow用户
提问于 2013-08-12 20:44:23
回答 1查看 5.5K关注 0票数 0

这很复杂(对我来说)是因为表的每一列都是从一个单独的MySQL表加载的,而且每个MySQL表都有不同数量的记录。一开始,我想我可以从左上角到右下角逐行逐个单元格生成html表,但这是行不通的,因为每个MySQL表都有不同的记录长度,这会生成格式错误的html表。你有什么意见建议?

到目前为止我的想法是:

  • 获取MySQL中所有表的列表,这些表确定列的数量
  • 从记录最多的表中获取计数
  • 使用参数(表的#作为列,max#作为行)创建一个表
  • 用相应的记录更新每个单元格,但不太确定如何更新

根据要求,一些代码:

代码语言:javascript
复制
$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>";

这是错误的,不仅因为它生成格式错误的表,还因为它将列转换为行.

如能提供任何帮助,将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-13 02:37:44

解决我最讨厌的问题:

代码语言:javascript
复制
$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>";
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18196374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档