0

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?

4 Answers 4

2

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
    ));
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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

Comments

0

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

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.