Postingan lainnya
Buku Ini Koding!
Baru!
Buku ini akan jadi teman perjalanan kamu belajar sampai dapat kerjaan di dunia programming!
json_decode to html
Permisi gan. ane mau buat profile page menggunakan Instagram API yg ada di githubnya mgp25. pas ane process, outputnya berupa array, nah ane mau ngambil salah satu array untuk ditrauh di table html ane. begini script ane gan:
<?php
$json_string = "REQUEST: users/4972387392/info/
RESPONSE: {"status": "ok", "user": {"usertags_count": 2, "has_anonymous_profile_picture": false, "following_count": 85, "usertag_review_enabled": false, "can_boost_post": false, "full_name": "mie rasa enak", "biography": "saya adalah mie amak kost", "is_profile_action_needed": false, "has_chaining": true, "media_count": 1, "auto_expand_chaining": false, "include_direct_blacklist_status": true, "follower_count": 40, "pk": 4972387392, "is_verified": false, "username": "akuuntukkamu", "geo_media_count": 0, "profile_pic_url": "http://scontent.cdninstagram.com/1642863562627279_t51.2885-19/11809984_1822533152_a.jpg", "can_see_organic_insights": false, "can_convert_to_business": false, "is_private": false, "is_business": false, "show_business_conversion_icon": false, "show_feed_biz_conversion_icon": false, "show_conversion_edit_entry": false, "show_insights_terms": false, "hd_profile_pic_url_info": {"url": "http://scontent.cdninstagram.com/t51.2885-19/1642863562627279_11809984_1822533152_a.jpg", "width": 150, "height": 150}, "is_needy": false, "external_url": ""}}";
$array = json_decode($json_string, true);
?>
<table border="1" cellpadding="10">
<thead><tr><th>Profile</th></tr></thead>
<tbody>
<?php foreach($array['user'] as $ambil => $info): ?>
<tr>
<td><?php echo $info['full_name']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
tapi hasilnya malah kosong gan, maksih atas perhatiannya, mohon dijawab bila mengerti :)
6 Jawaban:
kenapa gak json saja? lebih mudah membaca json dari pada array menurut saya dan itu $json_string nya gak json murni, yang murni itu mulai dari {} dan mungkin saja waktu decode nya jadi gagal
Jawaban Terpilih
Ini hasil json nya gan masih amburadul
{"status": "ok", "user": {"usertags_count": 2, "has_anonymous_profile_picture": false, "following_count": 85, "usertag_review_enabled": false, "can_boost_post": false, "full_name": "mie rasa enak", "biography": "saya adalah mie amak kost", "is_profile_action_needed": false, "has_chaining": true, "media_count": 1, "auto_expand_chaining": false, "include_direct_blacklist_status": true, "follower_count": 40, "pk": 4972387392, "is_verified": false, "username": "akuuntukkamu", "geo_media_count": 0, "profile_pic_url": "http://scontent.cdninstagram.com/1642863562627279_t51.2885-19/11809984_1822533152_a.jpg", "can_see_organic_insights": false, "can_convert_to_business": false, "is_private": false, "is_business": false, "show_business_conversion_icon": false, "show_feed_biz_conversion_icon": false, "show_conversion_edit_entry": false, "show_insights_terms": false, "hd_profile_pic_url_info": {"url": "http://scontent.cdninstagram.com/t51.2885-19/1642863562627279_11809984_1822533152_a.jpg", "width": 150, "height": 150}, "is_needy": false, "external_url": ""}}";
$array = json_decode($json_string, true);
Dan saya mau ambil data di json tersebut untuk dimasukkan di html seperti ini
<table border="1" cellpadding="10">
<thead><tr><th>Profile</th></tr></thead>
<tbody>
<?php foreach($array['user'] as $ambil => $info): ?>
<tr>
<td><?php echo $info['full_name']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
Gan ane kok nda bisa batalkan jadi jawaban terbaik, ane tepencet dan nda bisa dibatalkan nih
Ini bagaimana pertanyaan saya? apa tidak ada yg bisa jawab atau memang sengaja tidak dijawab? Hmmmm...
coba di vardump dulu $array'nya.. tampil datanya gak
kalo gak coba di pake kayak gini
<?php
$json_string = '{"status": "ok", "user": {"usertags_count": 2, "has_anonymous_profile_picture": false, "following_count": 85, "usertag_review_enabled": false, "can_boost_post": false, "full_name": "mie rasa enak", "biography": "saya adalah mie amak kost", "is_profile_action_needed": false, "has_chaining": true, "media_count": 1, "auto_expand_chaining": false, "include_direct_blacklist_status": true, "follower_count": 40, "pk": 4972387392, "is_verified": false, "username": "akuuntukkamu", "geo_media_count": 0, "profile_pic_url": "http://scontent.cdninstagram.com/1642863562627279_t51.2885-19/11809984_1822533152_a.jpg", "can_see_organic_insights": false, "can_convert_to_business": false, "is_private": false, "is_business": false, "show_business_conversion_icon": false, "show_feed_biz_conversion_icon": false, "show_conversion_edit_entry": false, "show_insights_terms": false, "hd_profile_pic_url_info": {"url": "http://scontent.cdninstagram.com/t51.2885-19/1642863562627279_11809984_1822533152_a.jpg", "width": 150, "height": 150}, "is_needy": false, "external_url": ""}}';
$array = json_decode($json_string, true);
?>
<table border="1" cellpadding="10">
<thead><tr><th>Profile</th></tr></thead>
<tbody>
<?php foreach($array as $ambil ){ ?>
<tr>
<td><?php echo $ambil["full_name"]; ?></td>
</tr>
<?php } ?>
</tbody>
</table>