我正试图用php数组中的数据生成一个表。
这是我的php代码:
<?php
$result = array();
ob_start();
include 'include/podcastRead.php';
$result = ob_get_clean();
?>
HTML表格:
<table class="mdl-data-table mdl-js-data-table" id="tablee">
<thead>
<tr>
<th>ID</th>
<th class="mdl-data-table__cell--non-numeric">Nome</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td class="mdl-data-table__cell--non-numeric">Nerdcast</td>
</tr>
</tbody>
</table>
$result数组有json数据,这里是:
{"podcast":[{"id":"1","nome":"Dan Carlin is Hardcore History"},{"id":"2","nome":"Dose Extra"}]}
我尝试了很多方法,在这里和其他网站上阅读和重读了很多帖子。我无法创建表。
我也尝试过一些插件,比如DataTables。
这看起来很简单。lol.
我该怎么做?谢谢
如果json_decode()
是$result
,则会得到一个对象,因此在$data->podcast
上运行foreach循环,并为每行输出一个表。
<?php
$data = json_decode($result);
echo '<table class="mdl-data-table mdl-js-data-table" id="tablee">
<thead>
<tr>
<th>ID</th>
<th class="mdl-data-table__cell--non-numeric">Nome</th>
</tr>
</thead>
<tbody>';
foreach ($data->podcast as $row) {
echo '<tr>
<td>'.$row->id.'</td>
<td class="mdl-data-table__cell--non-numeric">'.$row->nome.'</td>
</tr>';
}
echo '</tbody>
</table>';
?>