yegomo
yegomo

Reputation: 297

How to response a json string in Codeigniter

I have a database table named users with some keys 'userid, username, age', and also there are some records in this table, I want to get them like json, please look at this

   {
        "status":"1",
        "msg":"success",
        "userlist":[
        {
        "userid":"1",
        "username":"chard",
        "age":"22"
        },
        {
        "userid":"2",
        "username":"rose",
        "age":"21"
        },
        {
        "userid":"3",
        "username":"niki",
        "age":"25"
        }
        ]
}

user_model.php file, i write

function get_users()
{
 $query = $this->db->get('users');
     return json_encode($query->row_array());
}

user.php controller file, i write

function index_get()
{
  $this->load->model('users_model');
  $query = $this->users_model->get_users();
 echo $query;
}

I can get the result, but it's a wrong result, only this

{ "userid":"1", "username":"chard", "age":"22" }

so how should I fix this?

Upvotes: 0

Views: 186

Answers (4)

Gustavo Rubio
Gustavo Rubio

Reputation: 10827

I personally prefer objects:

$data = $query->result();
echo json_encode($data);

That will return an array of objects, instead of an array of arrays (multidimensional).

You can even pass a parameter value to that method to return those objects as instances of a specific class:

$data = $query->result('User_model');

I tend to use just the magic constant for that in any case I later change the class name:

$data = $query->result(__CLASS__);

That way you can access your objects in your javascript callbacks like this:

var data = yourResponse;
yourResponse.userlist.forEach(function(user){
  alert( user.username );
});

Upvotes: 0

Chandan Singh Gadhwal
Chandan Singh Gadhwal

Reputation: 197

You can use this:

 function get_users()
 {  
            $dataarr = array();
    $res = array();
    $this->db->start_cache();

    $query = $this->db->get('users');

    $this->db->stop_cache();
    $this->db->flush_cache();
    $groups = array();

    if(!empty($res))
    {   
                     foreach ($query->result() as $row)
                     {  
                        $dataarr[]=
            array(

            'userid' => $row['userid'],
            'username' => $row['username'], 
            'age' => $row['age']
                            );  
                     }                      
    }
    $query->free_result();
            $res = array(
                           'status' => 1,
                           'msg' => 'success',
                           'userlist' => $dataarr

                        );


    return json_encode($res);
    }

Upvotes: 0

Latheesan
Latheesan

Reputation: 24116

Try this:

function get_users()
{
   $query = $this->db->get('users');
   return $query->result_array();
}

function index_get()
{
    $this->load->model('users_model');
    $users = $this->users_model->get_users();
    echo json_encode(array(
        'status' => 1,
        'msg' => 'success',
        'userlist' => $users
    ));
}

Upvotes: 0

Nikunj Kabariya
Nikunj Kabariya

Reputation: 840

Try $query->result_array() instead of $query->row_array()

Your model function change it:

function get_users()
{
 $query = $this->db->get('users');
 return $query->result_array();
}

And in controller method

function index_get()
{
    $this->load->model('users_model');
    $users = $this->users_model->get_users();
    echo json_encode(array(
        'status' => 1,
        'msg' => 'success',
        'userlist' => $users
    ));
}

Upvotes: 2

Related Questions