2017-05-09 129 views
0

我是API和PHP的新手,但对于大学里的任务我不得不从API抽取数据并显示它。目前,我有下面的代码工作,但我无法弄清楚如何使它在一个表中单独显示每个口袋妖怪的名字,这样我可以添加图像和其他数据到每个细胞与它:将从API中提取的PHP数据放到HTML表格中

<?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>"; 
     } 
    ?> 

任何考生?我知道这可能是一个简单的解决方案,我只是这么新而且我尝试的所有东西都会破坏代码。

回答

0

为什么不使用html表?

<?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> 
+1

非常感谢!你完全回答我的问题,如果不超过要求! – Kat