cannot access query results in view: Message: Undefined variable - 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: cannot access query results in view: Message: Undefined variable (/showthread.php?tid=39262) |
cannot access query results in view: Message: Undefined variable - El Forum - 03-06-2011 [eluser]johnmerlino[/eluser] Hey all, when home/index page is loaded, this method is called: Code: public function index(){ This method is called in model: Code: public function find($param){ So result() returns an array of objects translated from field names. So I presume I can now access the property like $row->title. I also have this autoloaded template class: Code: public function __get($name){ But when I try to access the objects in view, I get undefined variable: Code: <h1><?php echo $head; ?></h1> alhough I am able to access $head and $subhead, which contained hard coded values declared in controller. I am not sure how I can access my database results in view. Thanks for response. cannot access query results in view: Message: Undefined variable - El Forum - 03-06-2011 [eluser]johnmerlino[/eluser] This fix for this was assigning the object that resulted from result() in an associative array of an array object: $data = $home->find('all'); $options['records'] = $data; At that point, the associatve array become available as a variable in view and that variable held an object of key/value pairs from the query, so foreach can be used to iterate through the objects indexed in the array (e.g. $row->title). |