Welcome Guest, Not a member yet? Register   Sign In
Issue defining variables in constructor
#1

[eluser]soarchrist[/eluser]
I am really lost on this one. I've read about it here and there... and have seen "examples" of how this should work.

The goal is to define the page title in the __construct() for ALL functions of the controller. In the view file "header", it just calls "echo @$title;"

What am I doing wrong?

Code:
class Contacts extends Controller {
    
    
    function __construct()
    {
        parent::Controller();
            
        $this->$data['title'] = 'Contacts Management';
        
        
    }
    
    function index()
    {
        
        $this->load->view('header');
        $this->load->view('loginbox');
        $this->load->view('menu');
        $this->load->view('awantay/contacts');
        $this->load->view('footer');
    }

Not only is the title NOT displayed in the page header, but actually errors out saying:
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: awantay/contacts.php

Line Number: 17

Fatal error: Cannot access empty property in C:\xampp\xampplite\htdocs\system\application\controllers\awantay\contacts.php on line 17

Line 17 refers to the line in the __construct:

Code:
$this->$data['title'] = 'Contacts Management';

ANY help is greatly appreciated.
#2

[eluser]Dyllon[/eluser]
You are not passing the data array to your view, and it's throwing an undefined error message because surprise, you haven't defined it.

Edit: Just noticed this was your first post, welcome to the forums.
#3

[eluser]soarchrist[/eluser]
[quote author="Dyllon" date="1266055323"]You are not passing the data array to your view, and it's throwing an undefined error message because surprise, you haven't defined it.

Edit: Just noticed this was your first post, welcome to the forums.[/quote]

Dyllon,

Thanks for the welcome-- and thanks for the advice. As a side note, my last name is Dillon.

I hear what you're saying, but I need specifics. How do I pass the data array to the view?

I've seen:

Code:
$this->load->view('header',$this->$data);
Code:
$this->load->view('header',$data);
Code:
$this->load->vars($this->$data);

NONE of these work. The error STILL points to line 17, which is in the __construct where it defines the $this->$data['title']. I think my problem needs to be solved on line 17 before I worry about passing it to the view. Any other ideas? How would you write it to pass the $data array to the view?

Thanks again for all your help!

Scott
#4

[eluser]Valdis Ozols[/eluser]
More like this:
Code:
$this->data['title'] = 'Contacts Management';

and same to use it:
Code:
$this->load->view('header', $this->data );
#5

[eluser]flaky[/eluser]
you need to declare $data first
do it like this
Code:
class Contacts extends Controller {
    
    private $data = array();

    
    function __construct()
    {
        parent::Controller();
            
        $this->$data['title'] = 'Contacts Management';
        
        
    }

//your code down there
#6

[eluser]soarchrist[/eluser]
You guys are great and I appreciate all of your help. The frustrating thing is that I have tried just about every stinkin' combination. Here's the code with your suggestions... and I'm getting the SAME error.

Code:
class Contacts extends Controller {
    
    private $data = array();
    
    
    function __construct()
    {
        parent::Controller();
        $this->$data['title'] = 'Contacts Management';
        
        
    }
    
    function index()
    {
        
        $this->load->view('header',$this->$data);
        $this->load->view('loginbox');
        $this->load->view('menu');
        $this->load->view('awantay/contacts');
        $this->load->view('footer');
    }
#7

[eluser]jayrulez[/eluser]
You should write $this->data... not $this->$data...
#8

[eluser]soarchrist[/eluser]
[quote author="jayrulez" date="1266101552"]You should write $this->data... not $this->$data...[/quote]

I should buy you a beer. Well done and thank you. I've been working with PHP for years and have done extensive stuff with classes, how could I make such a simple and stupid syntax error?!

Jay Rulez...
#9

[eluser]SitesByJoe[/eluser]
Why are you adding $data to $this anyway? (e.g. $this->data)

Is this what you would do to make $data available to all the controller functions?

I've always defined my page titles per function with a config file fallback.
#10

[eluser]Zorancho[/eluser]
Why don't you use
Code:
$this->load->vars($data)
. It's built in method from the Loader class, you can use it in the __construct method and all $data will be available to all of your methods, plus if you use it in a parent class constructor, like for example MY_Controller, it will be available in all of your application controllers.




Theme © iAndrew 2016 - Forum software by © MyBB