CodeIgniter Forums
Undefined Variable when using Views - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Undefined Variable when using Views (/thread-26645.html)



Undefined Variable when using Views - El Forum - 01-19-2010

[eluser]Brandon Beasley[/eluser]
Home Controller

Code:
<?php

class Home extends Controller {

    function Home(){
    
        parent::Controller();    
    }
    
    public function index()
    {
        
        $data = new stdClass();  // WORKING
        
              $data->trends = $this->twitter->search('trends'); // WORKING
        
        
        // print_r($data);
        
        $this->load->view('trends_view', $data);

The results of print_r when executed on Home is displaying the object, but the View is not getting the $data variable passed to it.

trends_view View
Code:
<?php print_r($data); ?>

This is what is returned:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: views/trends_view.php


This is my first post, go easy on me Smile


Undefined Variable when using Views - El Forum - 01-19-2010

[eluser]jwindhorst[/eluser]
As far as I know, $data should be an array not an object. Try
Code:
$data['trends'] = $this->twitter->search('trends');



Undefined Variable when using Views - El Forum - 01-19-2010

[eluser]Brandon Beasley[/eluser]
I changed to:

Code:
$data['trends'] = $this->twitter->search('trends');
        
$this->load->view('trends_view', $data);

Still getting error when accessing view:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: views/trends_view.php



The $data variable is accessible and contains the correct data when viewed within Home controller.


Undefined Variable when using Views - El Forum - 01-19-2010

[eluser]JHackamack[/eluser]
When passing an array to a view it strips down a layer.

Example;

$data['trends'] in controller becomes $trends in the view.


Undefined Variable when using Views - El Forum - 01-19-2010

[eluser]Brandon Beasley[/eluser]
Seems to be working now...

I pass the $data variable into the view and then I reference the $trend variable once in the view.

This was most likely simple, I just made it difficult.

Thanks for the help guys!