Welcome Guest, Not a member yet? Register   Sign In
Why am I running into "Message: Undefined variable: data" constantly?
#1

[eluser]Sven Delle[/eluser]
I seem to get this A LOT. I simply wanna pass data to a view like (in controller):

Code:
public function index()
{
$data['feeback'] = 'Image was uploaded successfully';

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

And from the view:

Code:
echo 'This is the create thumbnail view file: ' . $feedback;

That's what I get from reading the documentation, but I constantly run into not being able to pass that data. Now, WHAT is it that I'm not getting!?

I alse tried declaring a public array like (top of class just before constructor):

Code:
public $data = array();

And then add to the array like:

Code:
$this->data['variable'] = 'data';

But I simply cannot access ANY of the above methods data in a view ... arrrgh! I often use AJAX to retreive these views, can that be part of it?
#2

[eluser]Stefan Hueg[/eluser]
Congratulations, you have stumbled upon a misconception of the PHP language Smile Implicit variable declaration.

You will have to declare $data = array() before adding data to it. It's just a PHP notice, but something you should avoid. Declare that locally in your function right before adding data to it, not in your class.

In order to use the variables in your views you will have to pass them as a parameter:

Code:
$this->load->view('my_view', $data); //or
$this->parser->parse('my_view', $data);




Theme © iAndrew 2016 - Forum software by © MyBB