Hallo! Selamat Datang di Marketplace produk digital, Freelancer terbaik di indonesia (68414 Members, 793 Products, 5353 Topic, 13045 Comments)

Bagaimana cara Menampilkan Json ke view Codeigniter

Dibuat
Login Terakhir 3 Bulan lalu,
Telah Dilihat 8970 Kali
Machasin Nur Mengatakan : Sultan Account
  1. Assalamualaikum Warahmatullahi Wabarakatuh...
  2. Sy mau tanya, cara menampilkan data json ke view gimana ya? Data json tsb didapat dari hasil eksekusi url di controler. Barangkali ada yg punya contoh project, syukron

Ada 6 Jawaban

Bukhori Muslim
commented on 10 Des 2019 06:55:52
  1. Assalamualaikum,.. bisa dengan cara seperti ini :
  2. // Controllers...
  3. $url = 'https://xxx.com/api/xxx/';
  4. $header = array(
  5. 'Accept: application/json',
  6. 'Authorization: [apikey]', // Ganti [apikey] dengan API KEY Anda
  7. );
  8. $ch = curl_init();
  9. curl_setopt($ch3, CURLOPT_URL, $url);
  10. curl_setopt($ch3, CURLOPT_RETURNTRANSFER, 1);
  11. curl_setopt($ch3, CURLOPT_FOLLOWLOCATION, 1);
  12. curl_setopt($ch3, CURLOPT_HTTPHEADER, $header);
  13. curl_setopt($ch3, CURLOPT_POST, 1);
  14. $semua_data = curl_exec($ch);
  15.  
  16. if(curl_errno($ch)){
  17. return 'Request Error:' . curl_error($ch);
  18. }
  19. $data['semua_data'] = json_decode($semua_data);
  20.  
  21. // Views atau tampilkan data
  22. foreach($semua_data->message as $item){
  23. echo "<tr>
  24. <td>".$item->id_produk."</td>
  25. <td>".$item->nama_produk."</td>
  26. <td>".rupiah($item->harga)."</td>
  27. <td>".$item->stok."</td>
  28. </tr>";
  29. }
  30.  
  31. // Untuk ambil data satuan
  32. $url = 'https://xxx.com/api/xxx/';
  33. $get_content = file_get_contents($url);
  34. $json = json_decode($get_content, TRUE);
  35. $total_stok = $json['stok'];
  36. echo "$total_stok";
Machasin Nur
commented on 10 Des 2019 22:24:39
  1. Assalamualaikum,..
  2.  
  3. Afwan, hasil json url adalah data satuan, tp kok belum bisa ya. Berikut ini ane tuliskan view dan controlernya :
  4.  
  5. VIEW
  6.  
  7. <div id='form' class="main-page left">
  8. <div class="single-block">
  9. <div class="content-block main left">
  10. <div class="block">
  11. <div class="block-title">
  12. <h2>Isilah Data Anda Pada Form Dibawah Ini</h2>
  13. </div>
  14. <div class="block-content">
  15. <div id="writecomment">
  16. <form action="<?php echo base_url(); ?>cekdata/cek_nik/" method="POST" onSubmit="return validasi(this)" id="form_cek">
  17.  
  18.  
  19. <p class="contact-form-user">
  20. <label for="c_nik">NIK<span class="required">*</label>
  21. <input type="text" placeholder="NIK Anda" id="nik" value='<?php echo $nik ?>' name='nik' class="required"/>
  22. </p>
  23. <p class="contact-form-user">
  24. <label for="c_name">Nama<span class="required">*</label>
  25. <input type="text" placeholder="Nama Lengkap sesuai KTP" id="nama" value='<?php echo $nama ?>' name='nama' class="required"/>
  26. </p>
  27. <p class="contact-form-user">
  28. <label for="c_ibu">Nama Ibu<span class="required">*</label>
  29. <input type="text" placeholder="Nama Ibu Kandung sesuai KK" id="ibu" value='<?php echo $ibu ?>' name='ibu' class="required"/>
  30. </p>
  31.  
  32.  
  33.  
  34. <p><input type="submit" name="submit" class="styled-button" value="Cek Data"/></p>
  35.  
  36. </form>
  37. </div>
  38. </div>
  39.  
  40. <div class="container">
  41. <div class="main-content mag-content clearfix">
  42. <div class="row">
  43.  
  44.  
  45. <div class="col-md-12">
  46. <div id="hasil" class="comments-wrapper clearfix">
  47. <h3 class="block-title"><span>Hasil Pencarian</span></h3>
  48.  
  49. <div class="hasil-content">
  50.  
  51. //Tampilan hasil agar disini gimana ya?
  52. // NIK :
  53. // Nama :
  54.  
  55. </div>
  56. </div>
  57. </div>
  58. </div><!-- .row -->
  59. </div>
  60. </div>
  61. </div>
  62. </div>
  63. </div>
  64. </div>
  65.  
  66. CONTROLER
  67.  
  68. function cek_nik(){
  69. // Check form submit or not
  70. if($this->input->post('submit') != NULL ){
  71.  
  72. // POST data
  73. $postData = $this->input->post();
  74. $nik = $postData['nik'];
  75. $nama = $postData['nama'];
  76. $ibu = $postData['ibu'];
  77.  
  78. $url = 'http://112.78.170.170:8082/ws_server/get_json/cekdata/get_nama?USER_ID=ADMIN_CEK_DATA&PASSWORD=123&NIK='.$nik.'NAMA_LGKP='.$nama.'NAMA_LGKP_IBU='.$ibu;
  79. $get_content = file_get_contents($url);
  80. $json = json_decode($get_content, TRUE);
  81.  
  82. $hasil_nik = $json['content'][0]['NIK'];
  83. $hasil_nama = $json['content'][0]['NAMA_LGKP'];
  84.  
  85. echo "$hasil_nik ";
  86. echo "$hasil_nama ";
  87.  
  88. }
  89. }
  90.  
  91. Syukron atas jawabannya
Bukhori Muslim
commented on 11 Des 2019 07:39:05
  1. Assalamualaikum,.. Coba share prinscreen struktur/format json nya gan biar kelihatan, biar langsung kasih jawaban yang tepat. ^_^
Machasin Nur
commented on 11 Des 2019 21:51:37
  1. Assalamualaikum,..
  2.  
  3. Berikut ini data Jsonnya mas, syukron sebelumnya
Terdapat 1 File dilampirkan :
1. Hasil_Json.jpg (176 KB)
Bukhori Muslim
commented on 12 Des 2019 07:55:11
Salah satu jawaban terbaik,..
  1. Assalamualaikum,.. coba begini gan :
  2.  
  3. $obj = json_decode($response, true);
  4. for($i=0; $i < count($obj['content']['0']); $i++){
  5. $hasil_nik = $obj['content']['0'][$i]['NIK'];
  6. $hasil_nama = $obj['content']['0'][$i]['NAMA_LGKP'];
  7. }
Machasin Nur
commented on 14 Des 2019 20:40:52
  1. Assalamualaikum,..
  2. Alhamdulillah dah bisa gan, tapi yg belum bisa menambah parameter pada url nya, kode ini kok ga mau menghasilkan Json nya ya?
  3. $url = 'http://112.78.170.170:8082/ws_server/get_json/cekdata/get_nama?USER_ID=ADMIN_CEK_DATA&PASSWORD=123&NIK='.$nik.'NAMA_LGKP='.$nama.'NAMA_LGKP_IBU='.$ibu;
  4.  
  5. Parameternya ga jalan gan, mohon sarannya, syukron ????????
Maaf, Untuk Memberikan Komentar Anda Harus Login !!!

Online