Postingan lainnya
Kelas Premium!
Belajar bikin website dari nol sekarang
Gunakan kupon "lebihcepat" untuk diskon 25%!
Menampilkan data json
<html>
<head>
</head>
<body>
<table border="1">
<tr>
<th>No</th>
<th>Ounce</th>
<th>Gram</th>
<th>Kilogram</th>
</tr>
<?php
function main(){
$ch = curl_init();
$option = array(
CURLOPT_URL => "http://goldpricez.com/api/rates/currency/cad/measure/all",
CURLOPT_HTTPHEADER => array("X-API-KEY: e5d6***"),
CURLOPT_RETURNTRANSFER => true
);
curl_setopt_array($ch,$option);
$res = json_decode(curl_exec($ch));
curl_close($ch);
}
main();
for($a=0; $a < count($res); $a++)
{
echo "<tr>";
// penomeran otomatis
echo "<td>".$a."</td>";
// menayangkan
echo "<td>".$a[$res]['ounce_price_usd']."</td>";
echo "<td>".$a[$res]['gram_in_usd']."</td>";
echo "<td>".$a[$res]['kg_in_usd']."</td>";
print "</tr>";
}
?>
</table><br>
Gold Prices<br>
<a href="http://goldpricez.com/uk/gram"> Gold Rates </a>
</body>
</html>
Mau tanya, pengen ngeluarin data json ke bentuk table, tapi gak keluar.. udh nyoba2 di beberapa referensi gak berhasil, apa yang harus di benerin dan salahnya dimana?
Mohon pencerahannya.
Terimakasih
2 Jawaban:
<pre> $res = json_decode(curl_exec($ch), true); </pre>
response json nya kayak gmn ya?
Tanggapan
udah kak, udah di tambah true tetep gak berpengaruh..
bukannya kebalik ya? coba ini <pre> for($a=0; $a < count($res); $a++) { echo "<tr>"; // penomeran otomatis echo "<td>".$a."</td>"; // menayangkan echo "<td>".$res[$a]['ounce_price_usd']."</td>"; echo "<td>".$res[$a]['gram_in_usd']."</td>"; echo "<td>".$res[$a]['kg_in_usd']."</td>"; print "</tr>"; } </pre> tambahan, coba pake ini <pre> function main(){ $ch = curl_init(); $option = array( CURLOPT_URL => "http://goldpricez.com/api/rates/currency/cad/measure/all", CURLOPT_HTTPHEADER => array("X-API-KEY: e5d6***"), CURLOPT_RETURNTRANSFER => true ); curl_setopt_array($ch,$option);
$res = json_decode(curl_exec($ch));
curl_close($ch);
return $res; } $res = main(); </pre>
Tanggapan
Oh iyah kak kebalik.. *makasih koreksinya
Tapi gak keluar juga kak hasilnya :"
udh ane update tambahan