Accessing database value in view - 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: Accessing database value in view (/showthread.php?tid=11789) |
Accessing database value in view - El Forum - 09-23-2008 [eluser]patsm[/eluser] I'm new to Code Igniter and I've read all through the TOC and even have an existing application to use as a guide. However, I don't understand how to access a value from the database in one of my views. This is part of my controller Code: class classifieds extends Controller { This is the getFeatures function in the mdl_common Model as referenced in the Controller Code: function getFeatures() { The table I am accessing in my mySQL database is called features. I have two fields in that table called leftTopNav and leftBotNav. I would like to access the value of leftTopNav in my view in the file 'view.sidebar.php' (this is referenced in my controller and displays but I don't understand how to get the values. I've tried $features['leftTopNav'], I've tried $features[0], I've tried $leftTopNav along with all sorts of other things. I just don't get it!!! I know this is such a simple thing, but it has me stuck. Can someone tell me how to access this data in the form of a variable? Thanks! Accessing database value in view - El Forum - 09-23-2008 [eluser]GSV Sleeper Service[/eluser] $query->row() returns an object. you access this in your view by using $features->leftTopNav if you prefer the square bracket (array) notation, then use $query->row_array() Accessing database value in view - El Forum - 09-23-2008 [eluser]patsm[/eluser] That worked, thanks so much!!!! However, is there a better way to do this or am I doing it correctly? Accessing database value in view - El Forum - 09-23-2008 [eluser]Sumon[/eluser] If you are using PHP 5 then the most powerful and useful one is using Method Chaining. Use this as common syntax for all of your queries in model. Code: $query = $this->db->select('title') |