Welcome Guest, Not a member yet? Register   Sign In
Problem with model
#1

[eluser]Unknown[/eluser]
Hello,

I am a newbie to CI, just started. I want to make a blog application, and so I want to get all my dummy blog entries out of the database (made with scaffolding), but when i load the page i get this:

Code:
Blog heading

Fatal error: Call to a member function result() on a non-object in C:\wamp\www\ci\system\application\views\blog_view.php on line 18

This may be a noob-problem, but it's driving me nuts. Why do I get this error?

Here is my controller (blog.php):
Code:
<?php
class Blog extends Controller{

    function __construct()
    {
        parent::Controller();
        $this->load->helper('url');
    }

    function index()
    {
        // General
        $data['title'] = 'Blog title';
        $data['heading'] = 'Blog heading';
        
        // Load blog_model and save the get_all_entries function result
        $this->load->model('Blog_model');
        $data['entries'] = $this->Blog_model->get_all_entries();
        
        // Bypass Blog_model
        //$data['entries'] = $this->db->get('entries');
        
        // Load view(s)
        $this->load->view('blog_view', $data);
    }

    function comments()
    {
        $data['title'] = 'Comments title';
        $data['header'] = 'Comments heading';
    }
    
}
?>

Here is my model (blog_model.php):

Code:
<?php

class Blog_model extends Model{
    
    function Blog_model()
    {
        parent::Model();
    }

    function get_all_entries()
    {
        $query = $this->db->get('entries');
        return $query->result();
    }
    
}

?>

And here is my view (blog_view.php):
[code]
<html>
<head>
<title><?php echo $title;?></title>
<meta name="author" content="" />
<meta name="copyright" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta name="abstract" content="" />
<meta name="robots" content="index,follow" />
<meta name="language" content="" />
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
</head>

<body>

<h1>&lt;?php echo $heading;?&gt;</h1>

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

    <h2>&lt;?php echo $row['title'];?&gt;</h2>
    <p>&lt;?php echo $row['content'];?&gt;</p>
    
    <hr>
    
    &lt;?php endforeach; ?&gt;
    
&lt;/body&gt;
&lt;/html&gt;
[/code]

I really hope you can help me out with this. Thank you.

Best regards.
#2

[eluser]xwero[/eluser]
I wonder why don't you see the problem, it's a copy paste problem.
#3

[eluser]Colin Williams[/eluser]
Just iterate over $entries, not $entries->result(). You're already working with a result.
#4

[eluser]Unknown[/eluser]
D'oh :/. Thank you for your answers.

Best regards.




Theme © iAndrew 2016 - Forum software by © MyBB