Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter get_where
#1

[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()
{
    $this->load->view('account/files');
    
    $owner = $this->auth->get_user();
    
    return $this->db->get_where('files', array('owner =' => '$owner'))->result();
}

And in my view I have the following;

Code:
<?php foreach($query->result() as $row): ?>

    <span>&lt;?=$row->name?&gt;</span>

&lt;?php endforeach; ?&gt;

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
#2

[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();
#3

[eluser]doubleplusgood[/eluser]
Thanks rogierb. I changed that line and the view now displays Message: Undefined variable: files
#4

[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:
&lt;?php foreach($query as $row): ?&gt;
#5

[eluser]doubleplusgood[/eluser]
Hey noxie,

My controller now includes;

Code:
function files()
{
    $this->load->view('account/files');        
    $owner = $this->auth->get_user();        
    return $this->db->get_where('files', array('owner =' => '$owner'))->result();        
}

And my view is as follows;

Code:
&lt;?php foreach($query as $row): ?&gt;

    <div class="section">
        <span>&lt;?=$row->file_name?&gt;</span>    
    </div>

&lt;?php endforeach; ?&gt;

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
#6

[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()
{
    $this->load->view('account/files');        
    $owner = $this->auth->get_user();        
    return $this->db->get_where('files', array('owner =' => '$owner'))->result();        
}
[/quote]

Where do you call your view? If your view is 'account/files' change it to this:

Code:
// controller
    $owner = $this->auth->get_user();      
    // we store the output of the query into a data array
    $data['files'] = $this->db->get_where('files', array('owner =' => '$owner'))->result();  
    // we pass the data array to the view
    $this->load->view('account/files',$data);  

// view
    
    // we passed the data array to the view which now gives us access to 'files' where
    // our query result is stored
    &lt;?php foreach($files as $file): ?&gt;

    <div class="section">
        <span>&lt;?=$file->file_name?&gt;</span>    
    </div>

    &lt;?php endforeach; ?&gt;
#7

[eluser]doubleplusgood[/eluser]
Thanks man. I've obviously got some other issue because the page just displays blank. Probably some n00b issue.
#8

[eluser]n0xie[/eluser]
If you do a var_dump on the $data['files'], what does it say?
#9

[eluser]doubleplusgood[/eluser]
It just gives an empty array;

Array
(
)
#10

[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();
    var_dump($data['files']);




Theme © iAndrew 2016 - Forum software by © MyBB