![]() |
Help Need Controller and Accessing Object - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Help Need Controller and Accessing Object (/showthread.php?tid=58163) |
Help Need Controller and Accessing Object - El Forum - 05-21-2013 [eluser]Unknown[/eluser] Hello folks, I am new here and have a small issue. The problem is with understanding how i can access an result in controller. I got an error message like “Trying to get property of non-object” on line… I want to do some like this. function get_something($some_var){ $this->load->model('some_model','',TRUE); $data['query'] = $this->some_model->get_some_thing($some_var); if($data['query']->col2==some_value){ //<--this will be the line where i get the error. $this->load->view('view_1',$data); }else{ $this->load->view('view_2',$data); } } Nick Carangi pointed this out, i know my mistake but i don’t understand how i can access the value. normally in the view i do some like this foreach($query as $row): echo "col2: ".$row->col2; and so on but i want access the value in the controller the function in some_model looks like this… function get_some_thing($some_var){ $this->db->select('col1'); $this->db->select('col2'); $this->db->select('col3'); $this->db->where('col1',$some_var); $data = $this->db->get('some_table'); return $data->result(); } Help Need Controller and Accessing Object - El Forum - 05-21-2013 [eluser]Unknown[/eluser] 1. Make sure that your query inside your model method "get_some_thing", actualy returns something. If it doesn't than your tries to access the result object rows will always return the error of "Trying to access property of a non objec", since what the object $data will be equal then is a boolean value of "false" , since that is what is returned from your model if the query was not successful. 2.Try to do and IF statement like this : "if($data->col2 == "some value") { run some code here } else { some other code } (notice that there is no $data['query'], but $data on it's own Help Need Controller and Accessing Object - El Forum - 05-22-2013 [eluser]TheFuzzy0ne[/eluser] Welcome to the CodeIgniter forums! You're trying to use an array as an object. return $data->result() returns an array of objects, so you will need to loop through the array to access each object. If you're only expecting a single row of data, use row() instead of result(), which will pass back an object, and not an array of objects. |