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

时间:2018-07-19 19:32:05

标签: php json html-table

我正在运行一个python脚本,该脚本将JSON文件返回到某个目录。然后,我将这些文件中的数据插入HTML表中。一切工作正常,除了对于每个条目我得到3行,其中一个包含我需要的信息,另外两个为空白。我认为错误出在我的PHP代码中,但我似乎无法修复。任何帮助,将不胜感激! 我能够通过将td / tr标签上的填充设为0来消除显示行的过程,但我想以正确的方式解决此问题。

<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>

1 个答案:

答案 0 :(得分:0)

如前所述,只需将echo "</tr>";移到foreach之外。

<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>

希望对您有帮助,否则请用$testingvar_dump打印print_r,以查看数据是否正确。