I'm new to APIs and PHP but for an assignment at university I'm having to pull data from an API and display it. I currently have the following code working but I can't figure out how to make it display each Pokemon name separately in a table so that I can add images and other data into each cell with it:
<?php
$base = "http://pokeapi.co/api/v2/pokemon/";
for($id = 1; $id < 13; $id++){
$data = file_get_contents($base.$id.'/');
$pokemon = json_decode($data);
echo $pokemon->name."<br>";
}
?>
Any takers? I know it's probably an easy fix, I'm just so new to this and everything I try breaks the code.
Why not using an html table?
<?php
$base = "http://pokeapi.co/api/v2/pokemon/";
?>
<table>
<thead>
<th>Picture</th>
<th>Name</th>
</thead>
<tbody>
<?php
for($id = 1; $id < 13; $id++){
$data = file_get_contents($base.$id.'/');
$pokemon = json_decode($data);
echo '<tr>
<td>
<img src="'. $pokemon->sprites->front_default .'" width="30px" />
</td>
<td>
'. $pokemon->name .'
</td>
</tr>';
}
?>
</tbody>
</table>