I'm running a python script which returns JSON files to a certain directory. I am then inserting the data from these files into an HTML table. Everything is working fine except that for every entry I get 3 rows, one with the information I need and the other two are blank. I think the error is in my PHP code but I can't seem to fix it. Any help would be appreciated! I was able to eliminate showing the rows by making the padding on the td/tr tags 0 but I want to fix this the right way.
<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>
As mentioned before just move echo "</tr>";
outside of the 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>
I hope it helps, otherwise print your $testing
with var_dump
or print_r
to have a look if the data is correct.