leptoon leptoon - 1 year ago 171
HTML Question

Create HTML table from JSON array

I've been trying for hours to create a simple table with data from a JSON array.

Below is an example. I don't know why this code doesn't work. The error is:


"Trying to get property of non-object".


$json_string = file_get_contents("https://bittrex.com/api/v1/public/getmarkethistory?market=BTC-HYPER&count=5");

$array = json_decode($json_string);
?>

<table><tr><th>
<?php foreach($array as $o): ?>

<tr>
<td><?php $o->result->TimeStamp ?></td>
</tr>
<?php endforeach; ?>
</table>


Please look at the URL for json_string for formating.

Answer Source

Alternatively, you could add a second parameter to json_decode($json_string, true) to make it an array instead of an object. Consider this example:

<?php
$json_string = file_get_contents("https://bittrex.com/api/v1/public/getmarkethistory?market=BTC-HYPER&count=5");
$array = json_decode($json_string, true);

?>

<table border="1" cellpadding="10">
    <thead><tr><th>Timestamp</th></tr></thead>
    <tbody>
    <?php foreach($array['result'] as $key => $value): ?>
        <tr>
            <td><?php echo $value['TimeStamp']; ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download