![]() |
Getting error: Object of class CI_Output could not be converted to string - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Getting error: Object of class CI_Output could not be converted to string (/showthread.php?tid=64517) |
Getting error: Object of class CI_Output could not be converted to string - Kunal - 02-29-2016 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 !! RE: Getting error: Object of class CI_Output could not be converted to string - Narf - 02-29-2016 You're assigning the return value of set_output(), which is a CI_Output instance, to $data['json']. RE: Getting error: Object of class CI_Output could not be converted to string - siburny - 03-01-2016 You don't need to load a view if you are using set_output PHP Code: public function json_output(){ PHP Code: $this->output |