Yosafat Ksatria Yosafat Ksatria - 4 months ago 29
JSON Question

Converting JSON to HTML Table on CodeIgniter

I have some issue how converting JSON to HTML table.

Now I'm using CI as framework for PHP.

But the thing is I just need to know how can I converting this JSON type to HTML table.

I just need to get those in the HTML table.


  • the Currency name (e.g: AUD, USD)

  • and the "Jual, Beli" number. (e.g: AUD Jual: 9987.1 Beli 9907.11)




I have try to decode that JSON to array, but I still didn't get it..

Here's my array:

stdClass Object (
[Message] => stdClass Object (
[Code] => 200 [Message] => request success
)
[Data] => stdClass Object (
[AUD] => stdClass Object (
[Jual] => 9987.11 [Beli] => 9907.11
)
[CAD] => stdClass Object (
[Jual] => 10003.21 [Beli] => 9923.21
)
[CHF] => stdClass Object (
[Jual] => 13236.59 [Beli] => 13136.59
)
[CNY] => stdClass Object (
[Jual] => 1983.66 [Beli] => 1863.66
)
[DKK] => stdClass Object (
[Jual] => 1934.2 [Beli] => 1854.2
)
[EUR] => stdClass Object (
[Jual] => 14136.37 [Beli] => 14036.37
)
[GBP] => stdClass Object (
[Jual] => 16638.24 [Beli] => 16538.24
)
[HKD] => stdClass Object (
[Jual] => 1722.32 [Beli] => 1692.32
)
[JPY] => stdClass Object (
[Jual] => 122.67 [Beli] => 119.27
)
[NZD] => stdClass Object (
[Jual] => 9242.72 [Beli] => 9162.72
)
[SAR] => stdClass Object (
[Jual] => 3578.14 [Beli] => 3498.14
)
[SEK] => stdClass Object (
[Jual] => 1511.47 [Beli] => 1431.47
)
[SGD] => stdClass Object (
[Jual] => 9481.43 [Beli] => 9461.43
)
[USD] => stdClass Object (
[Jual] => 13277 [Beli] => 13261
)
)
[LastUpdate] => 2017-04-12 23:00:08
[ProcessingTime] => 0.0388939380646
)

Answer Source

Try "json_decode" (http://php.net/manual/en/function.json-encode.php) and the "foreach" loop construct (http://php.net/manual/en/control-structures.foreach.php), both which work regardless of the PHP framework you use:

$json_object = json_decode(file_get_contents('http://www.adisurya.net/kurs-bca/get')) ;

echo '<table>' ;
foreach ( $json_object -> Data as $currency => $data ) {

    echo '<tr>' ;
    echo '<td>' . $currency . '</td>' ;
    echo '<td>' . $data -> Jual . '</td>' ;
    echo '<td>' . $data -> Beli . '</td>' ;
    echo '</tr>' ;

}
echo '</table>' ;