简体   繁体   English

PHP创建动态HTML表

[英]php create dynamic html table

I need to create a dynamic html table using PHP after parsing a JSON file. 解析JSON文件后,我需要使用PHP创建动态html表。
I need this column structure for my table; 我的表需要这种列结构; Name | Status | Age | Count | Progress | Bad

How can I create a new row for every 'record' parsed from the JSON file (I can create a tab separated string). 如何为从JSON文件解析的每个“记录”创建新行(我可以创建制表符分隔的字符串)。
An additional difficulty is that some 'records' only contain data for the column 'Name' and others for all columns. 另一个困难是,某些“记录”仅包含“名称”列的数据,而另一些则包含所有列的数据。

So my question is how to add a row dynamically to the table and fill the right column ? 所以我的问题是如何在表中动态添加一行并填充右列?
( The key form the JSON file is the column header ) JSON文件的关键是列标题

Example of JSON format: JSON格式的示例:

{ 
"John":     {"status":"Wait" }, 
"Jennifer": {"status":"Active" }, 
"James":    {"status":"Active","age":56,"count":10,"progress":0.0029857,"bad":0} 
}

Something like this would work: 这样的事情会起作用:

$data = json_decode($json_string);
$columns = array();

echo "<table><tbody>";
foreach ($data as $name => $values) {
    echo "<tr><td>$name</td>";
    foreach ($values as $k => $v) {
        echo "<td>$v</td>";
        $columns[$k] = $k;
    }
    echo "</tr>";
}
echo "</tbody><thead><tr><th>name</th>";
foreach ($columns as $column) {
    echo "<th>$column</th>";
}
echo "</thead></table>"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM