CodeIgniter Forums
[Solved] Cannot get model in 'Hello World' project to work - getting undefined variable notice - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: [Solved] Cannot get model in 'Hello World' project to work - getting undefined variable notice (/thread-9712.html)



[Solved] Cannot get model in 'Hello World' project to work - getting undefined variable notice - El Forum - 07-06-2008

[eluser]wild thing[/eluser]
Source Code

Am trying to make a simple blogging app following the user guide on CodeIgniter 1.6.3. However, I keep getting the following error:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: posts

Filename: controllers/blahg.php

Line Number: 16

And the 'Failure' text is echoed, through my code. FYI, on line 26, $Posts_model doesn't work either (if I do $this->load->model('Posts_model');.

I've been pulling out my hair trying to figure out what I'm doing wrong. Help?


[Solved] Cannot get model in 'Hello World' project to work - getting undefined variable notice - El Forum - 07-06-2008

[eluser]therealmaloy[/eluser]
can you please post the blahg.php controller code if possible. so that we can take it from there.

i may have suggestions but i think we can thoroughly analyze this with your code posted.


[Solved] Cannot get model in 'Hello World' project to work - getting undefined variable notice - El Forum - 07-06-2008

[eluser]wild thing[/eluser]
Hi,

I had actually posted the entire source code with a link, but apologies, I guess it wasn't clear. I am now attaching the source with the post:

Code:
/*
General configuration:
config.php - $autoload['libraries'] = array('database', 'session');
routes.php - $route['default_controller'] = "blahg";
constants.php - define('WEBSITE_TITLE',                            'Blahg v0.1a');
database.php - db settings (username, db, password)
*/

//Controller Code - /application/controllers/blahg.php

<?php

class Blahg extends Controller {
    
    function Blahg() {
        parent::Controller();
    }
    
    function index() {
        
        //$data['page_title'] = WEBSITE_TITLE.' | Blahg List';
        
        $this->load->model('Posts_model', 'posts');
        
        if($posts)
            echo "Success";
        else
            echo "Failure";
        
        //$data['posts'] = $Posts_model->get_posts(10);
        
        //$this->load->view('blahg_view', $data);
    }
    
}

?>

//Model Code - /application/models/posts_model.php
<?php

class Posts_model extends Model {

    private $title = '';
    private $content = '';
    private $added = '';
    private $updated = '';
    
    function Posts_model() {
        parent::Model();
    }
    
    function get_posts($number_of_posts) {
        $query = $this->db->get('posts', $number_of_posts);
        return $query->result();
    }
    
}
?>

//View Code - /application/views/blahg_view.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;link href="css/core.css" rel="stylesheet" type="text/css" /&gt;
&lt;link href="css/posts.css" rel="stylesheet" type="text/css" /&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;&lt;?php echo $page_title; ?&gt;&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;!-- Blog Entries List  --&gt;
&lt;?php
//Load Blahg entries
    
    foreach($posts as $post)
    {
    ?&gt;
    
    <div>
        <h3>&lt;?php echo $post['title'] ?&gt;</h3>
        <div class="date">
        added &lt;?php echo $post['added']; ?&gt;
        &lt;?php
            if($post['updated'])
            {
        ?&gt;
         | updated &lt;?php echo $post['updated']; ?&gt;
        &lt;?php
            }
        ?&gt;
        </div>
    </div>
    
    &lt;?php
    }
    ?&gt;
&lt;/body&gt;
&lt;/html&gt;



[Solved] Cannot get model in 'Hello World' project to work - getting undefined variable notice - El Forum - 07-06-2008

[eluser]Michael Wales[/eluser]
Code:
if($this->posts) {
  echo "Success";
} else {
  echo "Failure";
}



[Solved] Cannot get model in 'Hello World' project to work - getting undefined variable notice - El Forum - 07-07-2008

[eluser]wild thing[/eluser]
Hi Michael,

Thanks for you reply.

$this->$posts doesn't work either - it's giving me the same error.

Any thoughts?


[Solved] Cannot get model in 'Hello World' project to work - getting undefined variable notice - El Forum - 07-07-2008

[eluser]wild thing[/eluser]
Whoops! I guess my reading skills require a major tuning! It's $this->posts, not $this->$posts! Dang! I guess I'm still a bit mystified about what's going on under the hood, and that's why it's not making sense for me yet...

Time to dig out tutorials on PHP classes, I guess Smile

Thanks for your help!


[Solved] Cannot get model in 'Hello World' project to work - getting undefined variable notice - El Forum - 07-07-2008

[eluser]therealmaloy[/eluser]
wild, controller should be like this with your coding approach:

&lt;?php

class Blahg extends Controller {

function Blahg() {
parent::Controller();
}

function index() {

$data['page_title'] = WEBSITE_TITLE.' | Blahg List';

$this->load->model('Posts_model','posts');

if($this->posts)
echo "Success";
else
echo "Failure";

$data['posts'] = $this->posts->get_posts(10);

$this->load->view('blahg_view', $data);
}

}

?&gt;


[Solved] Cannot get model in 'Hello World' project to work - getting undefined variable notice - El Forum - 07-08-2008

[eluser]wild thing[/eluser]
Thanks Maloy!