Conor Mackel Conor Mackel - 2 months ago 9
PHP Question

How to create a table from api data

I have been working on this for awhile and I am maybe making this more difficult than it is. Any shared knowledge would be very much appreciated.
My code calls data using an API, I was able to display it as a ordered list but I wanted to make it more presentable using a table format.
Ideally I the results to be displayed grouped by day.
Below is what I have created so far, but it is not outputting anything currently:

echo "<table>";
echo "<tr>";
echo "<th>Date</th>";
echo "<th>Number of Asteroids</th>";
echo "</tr>";

echo "<tr>";
foreach ($near_earth_object->close_approach_data as $close_approach) {

echo "<td>" . $close_approach->close_approach_date . "</td>";

foreach ($data->near_earth_objects as $date => $count) {

echo"<td>" . sizeof($count) . "</td></tr>";

}
}

echo "<tr>";
echo "<th>Asteroid Name</th>";
echo "<th>Asteroid Size</th>";
echo "<th>Velocity</th>";
echo "<th>Passing Earth By</th>";
echo "</tr>";

echo "<tr>";
foreach ($data->near_earth_objects->$date as $near_earth_object) {
echo "<td>" . $near_earth_object->name . " <a href='" . $near_earth_object->nasa_jpl_url . "'>" . $near_earth_object->nasa_jpl_url . "</td>";
echo "<td>" . $near_earth_object->estimated_diameter->meters->estimated_diameter_min . " metres</td>";
}

foreach ($near_earth_object->close_approach_data as $close_approach) {
echo "<td>" . $close_approach->relative_velocity->kilometers_per_hour . " km/h " . "</td><td>". $close_approach->miss_distance->kilometers . " km</td></tr> ";
}



echo"</table>";

Answer

You should avoid echoing HTML code whenever possible. You can use PHP Short tags <?= and ?> to inject PHP into your HTML code. This line

echo "<td>" . $close_approach->close_approach_date . "</td>";

can then be changed to

<td><?=$close_approach->close_approach_date?></td>

Furthermore I don't see any PHP opening and closing tags (<?php and ?>) to start and end your PHP code (although this may just be lacking in your example)

You're only looping through your table data <td> tags not your table row <tr> tags. You have to include these in your loop to keep creating a new table rows for every item in your loop:

foreach ($near_earth_object->close_approach_data as $close_approach) {
echo "<tr>";

Finally, PHP offers an alternative syntax for control structures. Check out the PHP documentation

PHP offers an alternative syntax for some of its control structures; namely, if, while, for, foreach, and switch. In each case, the basic form of the alternate syntax is to change the opening brace to a colon (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively.

You can use this to improve your existing code:

foreach ($data->near_earth_objects->$date as $near_earth_object) {
    echo "<td>" . $near_earth_object->name . " <a href='" . $near_earth_object->nasa_jpl_url . "'>" . $near_earth_object->nasa_jpl_url . "</td>";
    echo "<td>" . $near_earth_object->estimated_diameter->meters->estimated_diameter_min . " metres</td>";
    }

And turn it into something like this

foreach ($data->near_earth_objects->$date as $near_earth_object): ?>
    <td><?=$near_earth_object->name?> <a href='<?=$near_earth_object->nasa_jpl_url?>'><?=$near_earth_object->nasa_jpl_url?></td>
    <td><?=$near_earth_object->estimated_diameter->meters->estimated_diameter_min?> metres</td>
    <? endforeach; ?>