Json response in Codeigniter -
below code user list webservice's json response.
<?php if ( ! defined('basepath')) exit('no direct script access allowed'); class webservice extends ci_controller { function list_user() { $result_login = $this->db->get('user_registration')->result(); $response = array(); $response ["success"] = 1; $response ["message"] = "user list."; foreach($result_login $row) { $data = array(); $data['user id'] = $row->user_id; $data['name'] = $row->name; $data['email'] = $row->email; $data['mobile_number'] = $row->mobile_number; $data['password'] = $row->password; $output2 = json_encode(array('responsedata' => $data)); echo $output2; } } } ?>
in code if replace $data $response in json_encode can't $data's value. got json response in format. json response.
{ "responsedata": { "user id": "7", "name": "bhavin", "email": "bhavin123@gmail.com", "mobile_number": "123456789", "password": "abc" } }
but want json response in format.
{ "responsedata": { "success": 1, "data": [ { "user id": "7", "name": "test", "email": "test1@gmail.com", "mobile_number": "123456789", "password": "abc" }, { "user id": "8", "name": "test2", "email": "test2@gmail.com", "mobile_number": "123456789", "password": "abc" } ] } }
you need arrange array this
i update below code
$array_of_event = array() foreach($result_login->result_array() $row) { $array_of_event[] = $row; } $data['success'] = "1"; $data['data'] = $array_of_event; //need assign event here $response['responsedata'] = $data; echo json_encode($response);
Comments
Post a Comment