Welcome Guest, Not a member yet? Register   Sign In
Should work but doesn't
#1

[eluser]clintonbeattie[/eluser]
Hi,

I have my Model, Views and Controller set up to what appears to be correct, but I keep getting "Undefined index: intro" when the page loads.

Can you please look through the code and tell me what I've done wrong? I'm sure it's something simple.

Thanks for any help!!!

CONTROLLER
Code:
class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();    
    }
    
    function index()
    {
        $data['title'] = "My Blog";
        $data['mainf'] = $this->MBlog->getAllPosts();
        $data['main'] = 'blog';
        $this->load->vars($data);
        $this->load->view('template');
    }

MODEL
Code:
class MBlog extends Model {

    function MBlog()
    {
        parent::Model();    
    }
    
    function getAllPosts(){
        $data = array();
        $Q = $this->db->get('posts');
        if ($Q->num_rows() > 0){
        foreach ($Q->result_array() as $row){
            $data[] = $row;
        }
        }
        $Q->free_result();
        return $data;
    }
    
    
}

VIEW
This is the template...
Code:
<!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;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;&lt;?php echo $title; ?&gt;&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
<h1>My Blog</h1>
<div id="main">
&lt;?php $this->load->view($main); ?&gt;
</div >
&lt;/body&gt;
&lt;/html&gt;

This is blog.php which is embedded into the template...
Code:
&lt;?php
echo "<h2>".$mainf['intro']."</h2>";
?&gt;
#2

[eluser]pistolPete[/eluser]
You do not pass any data to the view "blog.php"!
Code:
&lt;?php $this->load->view($main); ?&gt;
#3

[eluser]clintonbeattie[/eluser]
I thought this embedded the blog.php into the $main variable in my template.php page and the data could be displayed like that?

Code:
$data['main'] = 'blog';

Please confirm if I'm totally wrong (Which is highly likely lol)

For example, if I use this code in my blog.php page it spurts out all the data...

Code:
&lt;?php
foreach ($blogposts as $key => $post){
    echo "<h4>".$post['title']."</h4>";
    echo "<p>".$post['intro']."</p>";
    echo "<p>".$post['text']."</p>";
}
?&gt;
#4

[eluser]pistolPete[/eluser]
If you want to access variables in all your views, you have to use:
Code:
$this->load->vars($array)

From http://ellislab.com/codeigniter/user-gui...oader.html:
Quote:This function takes an associative array as input and generates variables using the PHP extract function. This function produces the same result as using the second parameter of the $this->load->view() function above. The reason you might want to use this function independently is if you would like to set some global variables in the constructor of your controller and have them become available in any view file loaded from any function. You can have multiple calls to this function. The data get cached and merged into one array for conversion to variables.




Theme © iAndrew 2016 - Forum software by © MyBB