1

Below is the code for 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 as $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 my code if i replace $data with $response in json_encode then i can't get $data's value. I got json response in this format. JSON Response.

    {
        "responsedata": {
                  "User Id": "7",
                  "Name": "bhavin",
                  "Email": "[email protected]",
                  "mobile_number": "123456789",
                  "Password": "abc"
  }
}

But i want json response in this format.

{
       "responsedata":
        {
        "success": 1,
        "data": [
          {
                      "User Id": "7",
                      "Name": "test",
                      "Email": "[email protected]",
                      "mobile_number": "123456789",
                      "Password": "abc"
          },
          {
                      "User Id": "8",
                      "Name": "test2",
                      "Email": "[email protected]",
                      "mobile_number": "123456789",
                      "Password": "abc"
          }
        ]
      }
    }

1 Answer 1

5

You need to arrange Your array like this

I update below code

$array_of_event = array()
foreach($result_login->result_array() as $row)
{
$array_of_event[] = $row;
}
    $data['success'] = "1";
    $data['data'] = $array_of_event; //need to assign event here
    $response['responsedata'] = $data;

    echo json_encode($response);
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.