Reputation: 77
I am new in using Codeigniter as web api, I want to get this result
{"result":[{"id":"1","nama":"Orion","nomor":"08576666762"},{"id":"2","nama":"Mars","nomor":"08576666770"},{"id":"7","nama":"Alpha","nomor":"08576666765"}],"success":"1","message":"success"}
but instead I get this kind of result :
{"result":[[{"id":"1","nama":"Orion","nomor":"08576666762"},{"id":"2","nama":"Mars","nomor":"08576666770"},{"id":"7","nama":"Alpha","nomor":"08576666765"}]],"success":"1","message":"success"}
I wonder where am I get it wrong?
I am using codeigniter and my code below is from controller and models
m_server.php (modals)
<?php
Class M_server extends CI_Model {
function __construct(){
parent::__construct();
$this->load->database();
}
// buat view dashboard main
function dash_main1(){
$data = $this->db->query("
select *
from telepon
");
$result = array();
$result['result'] = array();
$result['success'] = "1";
$result['message'] = "success";
array_push($result['result'], $data->result());
return $result;
}
}
Rest_server.php (controller)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Rest_server extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('m_server');
}
public function index()
{
$this->load->helper('url');
$this->load->view('rest_server');
}
function dash_main1(){
$data=$this->m_server->dash_main1();
echo json_encode($data);
}
}
Upvotes: 0
Views: 58
Reputation: 1501
Remove this line (optional)
$result['result'] = array();
And change this line
$result['result'] = $data->result(); //result become the array
array_push
add an element to an existing array
Upvotes: 1