Welcome Guest, Not a member yet? Register   Sign In
Fatal error: Call to a member function on a non-object
#1

[eluser]hellboy[/eluser]
Hi Everyone,

Been using Ci for a couple of days now, and I'm pretty impressed so far, but seem to be stuck with a problem 'Fatal error: Call to a member function on a non-object' in one of my views. From what I can see the connection to the DB seems ok and the query is returning data.

Here's some code:

The Controller
Code:
<?php
class News extends Controller {

    function News()
    {
        parent::Controller();
        $this->load->scaffolding('articles');
    }

    function index()
    {
//        $this->load->view('newsview');
        $this->load->model('Articles_model');
        
        $data['head'] = "My Real Title";
        $data['heading'] = "My Real Heading";
        
        $data['query'] = $this->Articles_model->get_last_ten_articles();

        $this->load->view('newsview', $data);
    }
    
    function comments()
    {
        echo 'Look at this!';
    }
}
?>

The View
Code:
<html>
<head>
<title>Jagungal.info</title>
</head>
<body>
    <h1>Welcome to Jagungal</h1>
    <h2>&lt;?php echo $heading; ?&gt;</h2>
    <p>&lt;?php echo $head; ?&gt;</p>
    <hr />
    
    &lt;?php foreach($query->result() as $row): ?&gt;
    
    <h3>&lt;?=$row->title?&gt;</h3>
    <p>&lt;?=$row->summary?&gt;</p>
    
    &lt;?php endforeach;?&gt;
&lt;/body&gt;
&lt;/html&gt;

The Model
Code:
&lt;?php
class Articles_model extends Model {

    function Articles_model()
    {
        parent::Model();
    }
    
    function get_last_ten_articles()
    {
        $this->load->database();
        //$query = $this->db->get('articles');
        $query = $this->db->query('SELECT * FROM articles');
        if($query->num_rows() > 0){
            echo $query->num_rows().' Rows';
            return $query->result();
        } else {
            echo 'No Rows';
            return array();
        }
    }
    
}
?&gt;

The echo lines in the model there tell me that 2 rows are being returned form the query (which is spot on!), but I just can't seem to use the $query in the view properly.

I'm using $autoload['libraries'] = array('database'); in my autoload.php file and scaffolding seems to work fine, so I'd guess there's no problem in the database.php file.

Any advice greatly appreciated!
#2

[eluser]hellboy[/eluser]
Just a quick update. I changed the following in my controller:

Code:
$data['query'] = $this->Articles_model->get_last_ten_articles();
to:
Code:
$data['query'] = $this->db->get('articles');
And it seems to work fine after that. Presumably the model function get_last_ten_articles isn't returning a correct value properly.

Any ideas?
#3

[eluser]FrankieShakes[/eluser]
hellboy,

The error is actually being caused in this line of your view:

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

If you look at the model's "get_last_ten_articles()" method, your return is the actual "result set", so you would either change the view to be:

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

or, rather than returning the result set in the model, you could do:

Code:
function get_last_ten_articles()
    {
        $this->load->database();
        //$query = $this->db->get('articles');
        $query = $this->db->query('SELECT * FROM articles');
        if($query->num_rows() > 0){
            echo $query->num_rows().' Rows';
            return $query;   // return query object, and not result set
        } else {
            echo 'No Rows';
            return array();
        }
    }
#4

[eluser]hellboy[/eluser]
Thank you very much. It works prefectly now!
#5

[eluser]FrankieShakes[/eluser]
Anytime! Glad it worked out for you.




Theme © iAndrew 2016 - Forum software by © MyBB