php - How to response a json string in Codeingniter -
i have database table named users keys 'userid, username, age', , there records in table, want them json, please @ 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, write
function get_users() { $query = $this->db->get('users'); return json_encode($query->row_array()); }
user.php controller file, write
function index_get() { $this->load->model('users_model'); $query = $this->users_model->get_users(); echo $query; }
i can result, it's wrong result, this
{ "userid":"1", "username":"chard", "age":"22" }
so how should fix this? thanks
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 )); }
Comments
Post a Comment