Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Ground Controller Lvl 1
Message 1 of 2

generate array json

how to build array json with konstruktor example

[{"position":{"lat":"-7.8099835","lng":"110.3749879,16z"},"nama_produk":"Sample 1","letak_produksi":"Sample 1","icon":"http:\/\/103.19.180.69\/assets\/marker\/marker.png"},{"position":{"lat":"-7.8051137","lng":"110.3904599,15z"},"nama_produk":"Sample 2","letak_produksi":"Sample 2","icon":"http:\/\/103.19.180.69\/assets\/marker\/marker.png"}]

 

my code

$this->db->select('lat, lng, nama_produk, letak_produksi');
        $this->db->from('tb_produk'); 
        $query = $this->db->get();
        if($query->num_rows() > 0){
          return $query->result_array();
        }else{
          return 0;
        }

 

and it generate 

 

{"position":[{"lat":"-8.336782","lng":"115.645166","nama_produk":"Garam Amed Bali","letak_produksi":"Desa Purwakanthi, Kecamatan Abang, Kabupaten Karangasem"},{"lat":"-6.905020","lng":"107.853061","nama_produk":"Ubi Cilembu Sumedang","letak_produksi":"desa Cilembu, Kecamatan Pamulihan, Kabupaten Sumedang, Jawa Barat"},{"lat":"4.638773","lng":"96.857915","nama_produk":"Kopi Arabika Gayo","letak_produksi":"desa Wih Ponrak, Kecamatan Silih Nara, Kabupaten Aceh Tengah, Nangroe Aceh Darusallam"},{"lat":"-2.116272","lng":"106.096334","nama_produk":"Lada Putih Muntok","letak_produksi":"Bangka Belitung"},{"lat":"-6.616456","lng":"110.690883","nama_produk":"Mebel Ukir Jepara","letak_produksi":"Jepara"},{"lat":"3.901801","lng":"115.691359","nama_produk":"Beras Adan Krayan","letak_produksi":"Zona produksi beras Adan Krayan terletak di kecamatan Krayan dan Krayan Selatan, Kabupaten Nunukan, "}]}

 

1 Reply
Copilot Lvl 2
Message 2 of 2

Re: generate array json

$query->result_array() always return array with object

Maybe this will help
$this->db->select('lat, lng, nama_produk, letak_produksi');
$this->db->from('tb_produk');
$query = $this->db->get();
if($query->num_rows() > 0){
$list = $query->result_array();
$arr = array();
foreach($list as $row){
$arr[] = array('position' => $row);
}
return $arr;
}else{
return 0;
}