Reputation:
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.
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
)
Upvotes: 0
Views: 1918
Reputation: 62
If you're using AJAX to retrieve this data. The I would suggest to use JSON.parse function first then use each loop in the page.
If your table
<table id="mytable"></table>
JQUERY AJAX CODE
$.get("http://www.adisurya.net/kurs-bca/get", function(data, status) {
if (status == "success") {
var loopdata = JSON.parse(data);
$.each(loopdata.data, function(i, item) {
$('#mytable').append("<td>"+item+"</td>");
});
}
});
Upvotes: 0
Reputation: 433
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>' ;
Upvotes: 2
Reputation: 1718
You are converting JSON to object, not an array. The best way is to process JSON in your controller:
$json = file_get_contents('http://www.adisurya.net/kurs-bca/get');
$data['json_array'] = json_decode($json, true);
and pass array to your view (change the name of your view):
$this->load->view('homepage', $data);
In your view you echo your table:
if (!empty($json_array)) {
echo '<table>';
foreach ($json_array as $currency => $currency_data) {
echo '<tr>';
echo '<td>'.$currency.'</td>';
echo '<td>'.$currency_data['Jual'].'</td>';
echo '<td>'.$currency_data['Beli'].'</td>';
echo '</tr>';
}
echo '</table>';
}
Upvotes: 0