Welcome Guest, Not a member yet? Register   Sign In
Getting error: Object of class CI_Output could not be converted to string
#1

I am trying to get some output from my model in JSON format, i have used the CI_Output class to set the content type and output. But whenever i try to access the view from the url, it displays the data in JSON format, but i am also getting an error, which says "http://localhost/login/index.php/json/json_output". My code is as follows:

Controller class:
<?php
class Json extends CI_Controller{
    public function __construct() {
        parent::__construct();
        $this->load->model('json_model');
    }
    public function json_output(){
        $data['json'] = 
                $this->output
                ->set_content_type('application/json')
                ->set_output(json_encode($this->json_model->get_user(),true));
        $this->load->view('json_view',$data);
        
    }
}

Model class:
<?php
class Json_model extends CI_Model{
    public function __construct() {
        parent::__construct();
        $this->load->database('login');
    }

    public function get_user(){
        $query = $this->db->get('users');
        return $query->result();
    }
}

View class:
<?php 
echo $json;
//foreach ($json as $js){
//    echo $js->user_id;
//    echo $js->username;
//    echo $js->password;
//    
//}
    

?>

Any help would be appreciated !!
Reply
#2

You're assigning the return value of set_output(), which is a CI_Output instance, to $data['json'].
Reply
#3

(This post was last modified: 03-01-2016, 08:05 PM by siburny.)

You don't need to load a view if you are using set_output
PHP Code:
public function json_output(){ 
PHP Code:
$this->output
   
->set_content_type('application/json')
   ->
set_output(json_encode($this->json_model->get_user(),true));
 
   
Reply




Theme © iAndrew 2016 - Forum software by © MyBB