首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用PHP将多个JSON文件解析为HTML表--行太多

使用PHP将多个JSON文件解析为HTML表--行太多
EN

Stack Overflow用户
提问于 2018-07-20 03:32:05
回答 1查看 140关注 0票数 0

我正在运行一个python脚本,它将JSON文件返回到某个目录。然后,我将这些文件中的数据插入到一个HTML表中。一切都很好,除了每个条目都有3行,一行有我需要的信息,另外两行是空的。我认为错误在我的PHP代码中,但我似乎无法修复它。如果有任何帮助,我将不胜感激!我可以通过在td/tr标签上设置填充0来消除行的显示,但我想以正确的方式解决这个问题。

代码语言:javascript
复制
<style>

table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 0px;
}

tr:nth-child(even) {
    background-color: #dddddd;
}
</style>

</head>
<body>
<table>
  <thead>
    <tr>
       <th>IP Address</th>
       <th>Manufacturer</th>
       <th>Model</th>
       <th>BIOSFamily</th>
       <th>BIOSDate</th>
       <th>SerialNumber</th>
       <th>More Information</th>
    </tr>
  </thead>

  <tbody>
<?php
error_reporting(0);
$dir = "/Users/Administrator/Desktop/Reserve1";
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        foreach(glob("*.json") as $filename) {
            $data = file_get_contents($filename);
            $testing = json_decode($data, true);
            echo "<tr>";
            echo "<td>{$filename }</td>";
            foreach($testing as $row) {
                echo "<td>{$row['Comments']['Manufacturer']}</td>";
                echo "<td>{$row['Comments']['Model']}  </td>";
                echo "<td>{$row['Comments']['BIOSFamily'] }</td>";
                echo "<td>{$row['Comments']['BIOSDate'] }</td>";
                echo "<td>{$row['Comments']['SerialNumber']}</td>";
                echo "</tr>";
            }
        }
    }
    echo "</table>";
}
?>
</html>
EN

回答 1

Stack Overflow用户

发布于 2018-07-20 03:55:51

如前所述,只需将echo "</tr>";移出foreach即可。

代码语言:javascript
复制
<style>

table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 0px;
}

tr:nth-child(even) {
    background-color: #dddddd;
}
</style>

</head>
<body>
<table>
  <thead>
    <tr>
       <th>IP Address</th>
       <th>Manufacturer</th>
       <th>Model</th>
       <th>BIOSFamily</th>
       <th>BIOSDate</th>
       <th>SerialNumber</th>
       <th>More Information</th>
    </tr>
  </thead>

  <tbody>
<?php
    error_reporting(0);
    $dir = "/Users/Administrator/Desktop/Reserve1";
        if (is_dir($dir)) {
            if ($dh = opendir($dir)) {
                foreach(glob("*.json") as $filename) {
                    $data = file_get_contents($filename);
                    $testing = json_decode($data,true);
                    echo "<tr>";
                    echo "<td>{$filename }</td>";
                    foreach($testing as $row) {
                        echo "<td>{$row['Comments']['Manufacturer']}</td>";
                        echo "<td>{$row['Comments']['Model']}  </td>";
                        echo "<td>{$row['Comments']['BIOSFamily'] }</td>";
                        echo "<td>{$row['Comments']['BIOSDate'] }</td>";
                        echo "<td>{$row['Comments']['SerialNumber']}</td>";
                    }
                    echo "</tr>";
                }
            }
        }
?>
 </tbody>
</table>
</body>
</html>

我希望它能帮助你,否则就用var_dumpprint_r打印你的$testing,看看数据是否正确。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51430503

复制
相关文章

相似问题

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