Welcome Guest, Not a member yet? Register   Sign In
$data is undefined - example followed from video tutorial
#1

[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);
    }
}
#2

[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
#3

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

[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?
#5

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

[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
#7

[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.




Theme © iAndrew 2016 - Forum software by © MyBB