Avi Thour
Avi Thour

Reputation: 410

Show JSON Data in a Table using foreach loop

I am making a table which will fetch data from API. I am able to fetch the first value, How can i use foreach with my code? I am not able to do so.

My Table:

<table class="table table-bordered">
<thead>
    <tr>
        <th>Name</th>
        <th>Steam URL</th>
    </tr>
</thead>
<tbody>
    <tr>
        <th><?php echo $name;?></th>
        <th><?php echo $steam;?></th>
    </tr>
</tbody>
</table>

And My PHP Code:

$name = $json->submissions[0]->data->name;
$steam = $json->submissions[0]->data->steam;

It's showing first value correct but not able to get the second entry.

Upvotes: 2

Views: 582

Answers (1)

Neo Morina
Neo Morina

Reputation: 401

Try this one if it works!

<table class="table table-bordered">
<thead>
    <tr>
        <th>Name</th>
        <th>Steam URL</th>
    </tr>
</thead>
<tbody>
    <?php foreach ($json->submissions as $key => $submission): ?>
        <tr>
            <th><?php echo $submission->data->name;?></th>
            <th><?php echo $submission->data->steam;?></th>
        </tr>
    <?php endforeach ?>
</tbody>
</table>

Upvotes: 3

Related Questions