Marko
Marko

Reputation: 11002

Convert codeigniter query to json?

I want to convert a model query to json with json_encode, it doesn't work. But with a ordinary array it does.

 $arr = array("one", "two", "three");
       $data["json"] = json_encode($arr);

Output

 <?php echo "var arr=".$json.";"; ?>
 var arr=["one","two","three"];

But when I try to convert a query codeigniter throws an error. What is it with that? This is the error message:

A PHP Error was encountered Severity: Warning Message: [json] (php_json_encode) type is unsupported, encoded as null

And the converted "query" result = I mean model method is like this:

{"conn_id":null,"result_id":null,"result_array":[],"result_object":[],"current_row":0,"num_rows":9,"row_data":null} 

I try to do like this

 $posts = $this->Posts_model->SelectAll();
       $data["posts"] = json_encode($posts); 

By the way, the model and method works just fine when I do it without json_encode.

Something I'm propably doing wrong, but the question is what?

Upvotes: 14

Views: 50364

Answers (5)

Jai Narayan
Jai Narayan

Reputation: 89

As per latest CI standard use the following code in your controller file:

$this->output->set_content_type('application/json')->set_output(json_encode($arr));

Upvotes: 2

Therichpost
Therichpost

Reputation: 1815

Here is the working solution:

            $json_data = $this->home_model->home_getall();
            $arr = array();
            foreach ($json_data as $results) {
            $arr[] = array(
                   'id' => $results->id,
                   'text' => $results->name
                    );
            }
         //save data mysql data in json encode format       
          $data['select2data'] = json_encode($arr);

Upvotes: 0

Pro RZ
Pro RZ

Reputation: 26

Models (Post):

function SelectAll()
{
   $this->db->select('*');
   $this->db->from('post');
   $query = $this->db->get();
   return $query;
}

Controllers :

$data['post'] = $this->post->SelectAll()->result_array();
echo json_encode($data);

Result:

{"post":[{"id":"5","name":"name_of_post"}]}

Upvotes: 0

bamossza
bamossza

Reputation: 3926

public function lastActivity()
{
    header("Content-Type: application/json");
    $this->db->select('*');
    $this->db->from('table_name');
    $query = $this->db->get();

    return json_encode($query->result());
}

Upvotes: 4

Stephen Curran
Stephen Curran

Reputation: 7433

You appear to be trying to encode the CodeIgniter database result object rather than the result array. The database result object acts as a wrapper around the cursor into the result set. You should fetch the result array from the result object and then encode that.

Your model code appears to be something like this :

function SelectAll()
{
    $sql = 'SELECT * FROM posts';
    // Return the result object
    return $this->db->query($sql);
}

It should be more like this :

function SelectAll()
{
    $sql = 'SELECT * FROM posts';
    $query = $this->db->query($sql);
    // Fetch the result array from the result object and return it
    return $query->result();
}

This will return an array of objects which you can encode in JSON.

The reason you are getting an error trying to encode the result object is because it has a resource member variable that cannot be encoded in JSON. This resource variable is actually the cursor into the result set.

Upvotes: 26

Related Questions