Reading database data in a 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: Reading database data in a view (/showthread.php?tid=28505) |
Reading database data in a view - El Forum - 03-13-2010 [eluser]Tanag[/eluser] Really simple question here. I'm making a call to my database that grabs a single record based on a user ID. I can't seem to read that record on my view without using a loop though. Im sure I just don't have the really simple syntax right, but I can't get it. Here is my controller code: Code: $this->db->where('user_id', $data['users']); That works fine, and I can view the data if I do this: Code: <?php foreach($query->result() as $row): ?> But using a loop seems pointless. I know there will always be one record as the user_id is my unique field. So how can I read the data like $query->result() as $row does. Like I want something like $query->user_id. Any help would be great, thanks. Im sure its a simple solution and I am a dummy. Reading database data in a view - El Forum - 03-13-2010 [eluser]mohsin917[/eluser] use this in foreach foreach($query as $row): Reading database data in a view - El Forum - 03-13-2010 [eluser]mohsin917[/eluser] Sorry for the previous reply it will not work.. Code: $this->db->where('user_id', $data['users']); Code: <?php foreach($query as $row): ?> This should work.. Reading database data in a view - El Forum - 03-13-2010 [eluser]K-Fella[/eluser] If you only expect one row from the database then use: Code: $data['user'] = $query->row(); // using row() instead of result() Then in your view you can call: Code: $user->id // $user->name, etc No need for loops. Reading database data in a view - El Forum - 03-13-2010 [eluser]Tanag[/eluser] Hmm maybe Im doing something wrong as I still can't get it work. Controller: Quote:$this->db->where('user_id', $data['users']); View: Quote:<?=$user->gold?> gold And I get the error: Code: A PHP Error was encountered Reading database data in a view - El Forum - 03-13-2010 [eluser]K-Fella[/eluser] The 2nd line should read: Code: $query = $this->db->get(‘users’); Reading database data in a view - El Forum - 03-13-2010 [eluser]Tanag[/eluser] Perfect, thanks! |