CodeIgniter Forums
$data is undefined - example followed from video tutorial - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: $data is undefined - example followed from video tutorial (/showthread.php?tid=19384)



$data is undefined - example followed from video tutorial - El Forum - 06-05-2009

[eluser]deco10[/eluser]
So I suppose I must have done something in terms of configuration to cause this because it doesn't any sense otherwise.

$data is undefined when I go to the prod page. index.php/prod
The home page loads just fine.

this is in my prod_view.php file (in views, obviously)

Code:
<title><?=$data['title']?></title>


this is my controller:

Code:
class Home extends Controller {

    function Home()
    {
        parent::Controller();
    $this->load->helper('url');
    $this->load->helper('html');    
    }
    
    function prod()
    {
        $data['title'] = "Auto - Product Line";

        $this->load->view('prod_view', $data);
    }
    
    
    function index()
    {
        $data['title'] = "Auto - Home";
        $this->load->view('home_view', $data);
    }
}



$data is undefined - example followed from video tutorial - El Forum - 06-05-2009

[eluser]wiredesignz[/eluser]
The $data array is extract()'ed into the view output buffer.

Simply use the array element names in the view. ie: $title


$data is undefined - example followed from video tutorial - El Forum - 06-05-2009

[eluser]deco10[/eluser]
Shame on me, I knew that, I remember hearing that specifically in the tutorial.
Thanks,


$data is undefined - example followed from video tutorial - El Forum - 06-05-2009

[eluser]deco10[/eluser]
Next question then, what if its a multi dimensional array?

In the views guide it shows about using a foreach loop, which is pretty obvious, but I don't want a foreach loop.

in my controller:

$data['lines']= array();
$data['lines']['first'] = "first el";
$data['lines']['second'] = "second el";

So in my view I use this by:
echo $lines['first'];

Which doesn't work. How do I access the information in $data['lines'] while in my view?


$data is undefined - example followed from video tutorial - El Forum - 06-05-2009

[eluser]Dam1an[/eluser]
That should work, what do you get if you do a var dump on $lines in the view?


$data is undefined - example followed from video tutorial - El Forum - 06-05-2009

[eluser]deco10[/eluser]
This is the output. I'm probably doing something stupid

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: lines

Filename: views/prod_view.php

Line Number: 46
NULL


$data is undefined - example followed from video tutorial - El Forum - 06-05-2009

[eluser]deco10[/eluser]
Newbie error. I had a controller that I didn't need and the name confused me. Sorry for the trouble. Thank you for your continued patience.