我正在运行一个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>
发布于 2018-07-20 03:55:51
如前所述,只需将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>
我希望它能帮助你,否则就用var_dump
或print_r
打印你的$testing
,看看数据是否正确。
https://stackoverflow.com/questions/51430503
复制相似问题