1

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.

1 Answer 1

2

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>';
?>
Sign up to request clarification or add additional context in comments.

1 Comment

I was sure it was not difficult. But I wasn't successful. It worked. Thank you @Tristan.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.