![]() |
CodeIgniter get_where - 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: CodeIgniter get_where (/showthread.php?tid=23605) Pages:
1
2
|
CodeIgniter get_where - El Forum - 10-16-2009 [eluser]doubleplusgood[/eluser] Hi there, I'm attempting to use get_where to grab a list of all database records where the owner is equal to the logged in user. This is my function in my controller; Code: function files() And in my view I have the following; Code: <?php foreach($query->result() as $row): ?> When I try accessing the view, I get the Fatal error: Call to a member function result() on a non-object in /views/account/files.php on line 1. Wondered if anyone had any ideas of what might be up with this? Thanks CodeIgniter get_where - El Forum - 10-16-2009 [eluser]rogierb[/eluser] change Code: $this->db->get_where('files', array('owner =' => '$owner'))->result(); to Code: return $this->db->get_where('files', array('owner =' => '$owner'))->result(); CodeIgniter get_where - El Forum - 10-16-2009 [eluser]doubleplusgood[/eluser] Thanks rogierb. I changed that line and the view now displays Message: Undefined variable: files CodeIgniter get_where - El Forum - 10-16-2009 [eluser]n0xie[/eluser] If you return the result object you can't loop over it in your view. So either return the MySQL object like this: Code: return $this->db->get_where('files', array('owner =' => '$owner')); or change your view: Code: <?php foreach($query as $row): ?> CodeIgniter get_where - El Forum - 10-16-2009 [eluser]doubleplusgood[/eluser] Hey noxie, My controller now includes; Code: function files() And my view is as follows; Code: <?php foreach($query as $row): ?> But I get the errors; A PHP Error was encountered Severity: Notice Message: Undefined variable: query Filename: account/files.php Line Number: 1 A PHP Error was encountered Severity: Warning Message: Invalid argument supplied for foreach() Filename: account/files.php Line Number: 1 CodeIgniter get_where - El Forum - 10-16-2009 [eluser]n0xie[/eluser] [quote author="Hanabi" date="1255710455"]Hey noxie, My controller now includes; [/quote] Aah I read too fast, I was under the assumption you were using a model. Since you are calling the database directly from the controller you don't need to return the data. [quote author="Hanabi" date="1255710455"] Code: function files() Where do you call your view? If your view is 'account/files' change it to this: Code: // controller CodeIgniter get_where - El Forum - 10-16-2009 [eluser]doubleplusgood[/eluser] Thanks man. I've obviously got some other issue because the page just displays blank. Probably some n00b issue. CodeIgniter get_where - El Forum - 10-16-2009 [eluser]n0xie[/eluser] If you do a var_dump on the $data['files'], what does it say? CodeIgniter get_where - El Forum - 10-16-2009 [eluser]doubleplusgood[/eluser] It just gives an empty array; Array ( ) CodeIgniter get_where - El Forum - 10-16-2009 [eluser]n0xie[/eluser] If you change your code to this does that help? Code: $data['files'] = $this->db->get_where('files', array('owner' => $owner))->result(); |