I want display json output to html table, but the table have much empty records. (photo below)
<?php
$json2=file_get_contents("****" . $row["Kenteken2"]);
$data2 = json_decode($json2);
if (strpos($data2[0], 'model') !== false) {
echo '<h4>Zelfde serie voertuigen</h4>';
// Open the table
echo '<table class="GeneratedTable">';
echo '<thead><tr><th>Niet DB</th><th>In DB</th></tr></thead><tbody><col style="width:50%" span="2" />';
// Cycle through the array
foreach ($data2 as $vrt) {
// Output a row
echo '<tr>';
echo '<td><a href="/****.php?ktk='.$vrt->model.'&via=ktk.net/'.$row["Kenteken2"].'&model='.$row["MerkModel"].'" target="_blank">'.$vrt->model .'</a></td>';
echo '<td><a href="/****.php?ID='.$vrt->id.'">'.$vrt->model2 .'</a> '.$vrt->dienst . ' ' . $vrt->bijzonderheden .'</td>';
echo "</tr>";
}
// Close the table
echo "</tbody></table>";
}
?>
result:
My Answer:
To display JSON output in an HTML table with empty records, you can use PHP to loop through the JSON data and create table rows for each record. Here's an example code snippet to achieve this:
php
$json_data = '[
{"id": 1, "name": "John Doe", "age": 30},
{"id": 2, "name": "Jane Smith", "age": 25},
{"id": 3, "name": "", "age": null},
{"id": 4, "name": "Mike Johnson", "age": 35}
]';
$data = json_decode($json_data, true);
echo '';
echo 'ID Name Age ';
foreach ($data as $row) {
echo '';
echo '' . $row['id'] . ' ';
echo '' . ($row['name'] ?? '') . ' ';
echo '' . ($row['age'] ?? '') . ' ';
echo ' ';
}
echo '
';
?>
In this code snippet, we first decode the JSON data using `json_decode()` function with the second parameter set to `true` to convert JSON objects into associative arrays. Then, we loop through each record in the JSON data and create table rows with the ID, Name, and Age values. We use the null coalescing operator (`??`) to handle empty or null values in the JSON data and display an empty string in the table cell.
You can replace the `$json_data` variable with your actual JSON data to display it in the HTML table.
Rate this post
5 of 5 based on 4645 votesComments