I'm trying to generate a table with data from an php array.
Here is my php code:
<?php
$result = array();
ob_start();
include 'include/podcastRead.php';
$result = ob_get_clean();
?>
HTML table:
<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>
The $result array have json data, here it is:
{"podcast":[{"id":"1","nome":"Dan Carlin is Hardcore History"},{"id":"2","nome":"Dose Extra"}]}
I have tried many ways, read and re-read so much posts here and other sites. I can't create a table.
I have also tried some plugins like DataTables.
It appears to be simple. lol.
How can I do it? Thanks.
If you json_decode()
the $result
you will get an object, so you then run a foreach loop on the $data->podcast
and output a table for each row.
<?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>';
?>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments