CodeIgniter Forums
Passing 2 Variables to your view error - 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: Passing 2 Variables to your view error (/thread-43857.html)



Passing 2 Variables to your view error - El Forum - 07-25-2011

[eluser]BenSeagrave[/eluser]
I googled how to pass 2 variables to your view file and someone told me to make an assoc array and include the variables inside of the array and pass the assoc array to the view. But i'm getting an error, Can someone tell me what i'm doing wrong?

Here is my controller :

Code:
<?php

class Site extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
    }
    
    function index()
    {
        $pageInfo = array(
            'main_content' => 'home_view',
            'document_title' => 'Home'
        );
        
        if($query = $this->site_model->getRecords())
        {
            $dbData['records'] = $query;
        }
        
        $data = array(
            'pageInfo' => $pageInfo,
            'dbData' => $dbData
        );
        
        $this->load->view('includes/template', $data);
    }
}

?>

The includes/template view file is just:

Code:
<?php

$this->load->view('includes/header');

$this->load->view($main_content);

$this->load->view('includes/footer');

?>

This worked before I had the assoc array passing both the varibles.


and this is the part of the view file that is loaded as the main_content at the moment:

Code:
<article>
        &lt;?php if(isset($records)) : foreach($records as $row): ?&gt;
        <h2>
        &lt;?=echo $row->title;?&gt;
        <h6>Posted on <strong>&lt;?=echo $row->date;?&gt;</strong> at <strong>&lt;?=echo $row->time;?&gt;</strong> | 10 Comments</h6>
        <p>&lt;?=echo $row->body;?&gt;</p>

        </h2>
    </article>


This is only a snippet of the main content as the rest of the file is pointless.


The error i'm getting from my browser is:

A PHP ERROR WAS ENCOUNTERED

Severity: Notice

Message: Undefined variable: main_content

Filename: includes/template.php

Line Number: 5



I don't understand what is wrong!! Can someone help Tongue


Passing 2 Variables to your view error - El Forum - 07-25-2011

[eluser]portgaz[/eluser]
Code:
&lt;?php

$this->load->view('includes/header');

$this->load->view($pageInfo['main_content']);

$this->load->view('includes/footer');

?&gt;

Code:
<article>
        &lt;?php if(isset($dbData['records'])) : foreach($dbData['records'] as $row): ?&gt;
        <h2>
        &lt;?=echo $row->title;?&gt;
        <h6>Posted on <strong>&lt;?=echo $row->date;?&gt;</strong> at <strong>&lt;?=echo $row->time;?&gt;</strong> | 10 Comments</h6>
        <p>&lt;?=echo $row->body;?&gt;</p>

        </h2>
    </article>

I never tested this but i hope this helps! Tongue


Passing 2 Variables to your view error - El Forum - 07-25-2011

[eluser]Mirge[/eluser]
portgaz is correct.

Since you're wondering why you're not able to retrieve data that you THINK you should be able to access, you'd start generally by looking where you're loading the view:

Code:
$this->load->view('includes/template', $data);

Then, seeing that you ARE passing $data... you'd see what $data contained.

Code:
print_r($data);

And there, you would have found your answer Smile