0

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);

    }
}

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.