Welcome Guest, Not a member yet? Register   Sign In
undefined variable (newbie)
#1

[eluser]jorre[/eluser]
I'm having troubles passing data from my controller to my views...
I keep getting an "undefined variable" error and can't spot my error...

I'm using the documentation from the user guide for this, but without any luck.
Who can help me out with this newb problem?

In my controller I have the following:

Code:
if($this->validation->run() == FALSE)
            {
                //SHOW STEP 2
                $this->load->view('header');        
                $data['title'] = "My Real Title";
                $data['heading'] = "My Real Heading";        
                $this->load->view('pollcreate_step2',$data);    //reload step 2
                $this->load->view('footer');    
            }

and in my view file, I have this:

Code:
<?=$heading;?>

produces the following error

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: heading

Filename: views/pollcreate_step2.php
#2

[eluser]tonanbarbarian[/eluser]
post the entire controller and view

some of the possible problems
- method names using reserved words
- view called in multiple places without data
#3

[eluser]thatscriptguy[/eluser]
I'm having the exact same problem. Experienced PHP scripter here, but brand new to Code Igniter. I'm following the tutorial to the letter, but I'm getting undefined variable notices.

Controller blog.php

Code:
<?php
class Blog extends Controller
{
    function index()
    {
        $data['title'] = 'My Real Title';
        $data['heading'] = 'My Real Heading';
        $this->load->view('blogview');
    }
}
?>

View blogview.php
Code:
<html>
<head><title><?php echo $title; ?></title>
</head>
<body>
<h1>&lt;?php echo $heading; ?&gt;</h1>
&lt;/body&gt;
&lt;/html&gt;

If it matters, I'm running the latest PHP (5.2.5, if I recall correctly)

The only bad thing showing in the logs is the notice...

Kevin
#4

[eluser]tonanbarbarian[/eluser]
you need a constructor for your controller

either

(any version of php)
Code:
&lt;?php
class Blog extends Controller
{
    function Blog() {
      parent::Controller();
    }

    function index()
    {
        $data['title'] = 'My Real Title';
        $data['heading'] = 'My Real Heading';
        $this->load->view('blogview');
    }
}


(php five only)
Code:
&lt;?php
class Blog extends Controller
{
    function __construct()
      parent::Controller();
    }
    function index()
    {
        $data['title'] = 'My Real Title';
        $data['heading'] = 'My Real Heading';
        $this->load->view('blogview');
    }
}
#5

[eluser]thatscriptguy[/eluser]
Unfortunately, that did not correct my notices. Also - The user guide at http://ellislab.com/codeigniter/user-gui...views.html

does not say that the constructor is a requirement. That should probably be added Wink

Either way, my pages are still throwing notices, using the exact code from the user guide.

Any other ideas?

Kevin
#6

[eluser]wiredesignz[/eluser]
@jorre: The notice is indicating the view file pollcreate_step2.php has the problem. Post the code for the view.

EDIT:

@thatscriptguy: You're not passing the $data array to your view.

Try:
Code:
&lt;?php
class Blog extends Controller
{
    function __construct()
      parent::Controller();
    }
    function index()
    {
        $data['title'] = 'My Real Title';
        $data['heading'] = 'My Real Heading';
        $this->load->view('blogview', $data);
    }
}
#7

[eluser]thatscriptguy[/eluser]
Actually, the OP and myself are different people. I did a google search for codeigniter undefined variable and this thread was the first result, and his post looked eerily similar to my problem, which is why I posted in this thread.

I am walking through the user guide/tutorial and am currently stuck at http://ellislab.com/codeigniter/user-gui...views.html

The code that I'm attempting to run is coming from the section named Adding Dynamic Data to the View on that page.

That's what is throwing me. I am following this user guide to the "T" and I'm still getting notices here..

Kevin
#8

[eluser]John_Betong[/eluser]
&nbsp;
Hi jorre and thatscriptguy,

Looking at the code it appears that your code should be re-arranged so that the variables $title, $heading are declared before loading the view.

Also $this->load->view(...) generates and outputs a string.

I prefer using the $result variable and then to echo the $result

Try this code:
&nbsp;

Code:
if($this->validation->run() == FALSE)
{
   //SHOW STEP 2
   $data['title']   = "My Real Title";
   $data['heading'] = "My Real Heading";        

   $result  = $this->load->view('header', $data, TRUE);        
   $result .= $this->load->view('pollcreate_step2',$data, TRUE);    //reload step 2
   $result .= $this->load->view('footer', $data, TRUE);    
  
   echo $result; // EDIT: CORRECTED VARIABLE NAME from $output
}

&nbsp;
&nbsp;
If on the offchance this does not work then:
1. take small bites at the code
2. get a "Hello World" to output with no errors
3. gradually increase the complexity.
&nbsp;
&nbsp;
#9

[eluser]wiredesignz[/eluser]
Read my edit above.

John is still in CI 1.5.4 land, CI 1.6 buffers output so concatenating views into a variable is no longer needed.

@John: your'e adding the views into a variable named $result and then echoing a variable named $output, thats just weird dude Tongue

Quote:if on the offchance this does not work then:
I'd say there's a good chance!
#10

[eluser]thatscriptguy[/eluser]
Sigh...Thanks wired...

5 years programming, and I was forgetting to pass the array..

Thanks again.




Theme © iAndrew 2016 - Forum software by © MyBB