Welcome Guest, Not a member yet? Register   Sign In
Passing part of the $data array in the constructor
#1

[eluser]Media Gearhead[/eluser]
I am trying to optimize my code and when loading views I am passing a data array.
Code:
class Auth extends Controller {
function index(){
    $data = array(
        'page_title' => 'Admin Login',
        'activeclass' => 'Login',
        'adminbase' => '/admin/auth',
        'right_header' => 'Login Info'
    );
    $this->load->view('admin/main',$data);
}
}

What I would like to do is something like this where any parts of the $data array that are the same on all parts of this controller are put in one location in the controller instead of copying it into every function but I am not able to figure it out.

Code:
class Auth extends Controller {
function __construct(){
    $data = array(
        'activeclass' => 'Login',
        'adminbase' => '/admin/auth',
        'right_header' => 'Login Info'
    );
}
function index(){
    $data .= array(
        'page_title' => 'Admin Login'
    );
    $this->load->view('admin/main',$data);
}
}

I am getting variable non-existent errors after I am loading the view. I am thinking it has to do with the scope of the variables but I am not finding the right setup to make it work.

Thank you for any help.
#2

[eluser]n0xie[/eluser]
Make it a class property:
Code:
// change
$data = ...

// to
$this->data

You might want to take a look at Base Classes for an even better approach
#3

[eluser]Media Gearhead[/eluser]
I love the idea. Unfortunately it is saying undefined variable. How can I instantiate the variable so that I can use it?

I am assuming something after the class is created.

Code:
class Auth extends Controller {
something $data something;

I am new to OOP and variable scope is one thing that is kicking me while down.
#4

[eluser]Buso[/eluser]
as noxie said, use $this->data

Code:
class Auth extends Controller {
  function __construct(){
    $this->data = array(
        'activeclass' => 'Login',
        'adminbase' => '/admin/auth',
        'right_header' => 'Login Info'
    );
  }
  function index(){
    $this->load->view('admin/main',$this->data);
  }
}
#5

[eluser]Media Gearhead[/eluser]
That is what I tried. This is the exact code I am using now. I had sent a simpler chunk of code to streamline the functionality I am trying to use.

Code:
<?php
class Auth extends Controller {
    
    function __construct(){
        parent::Controller();
        if($this->session->userdata('is_admin')){
            header('location: /admin/main');
        }else{
        }
        $this->$data = array (
            'page_title' => 'Admin Login',
            'activeclass' => 'Login',
            'adminbase' => '/admin/auth',
            'right_header' => 'Login Info'
        );

    }
    
    function index(){
    $this->$data .= array(
        'page_title' => 'Admin Login'
    );
        $this->load->view('admin/includes/head',$this->$data);
        $this->load->view('admin/includes/header',$this->$data);
        //$this->load->view('admin/includes/left',$this->$data);
        $this->load->library('table');
        $this->load->view('admin/login',$this->$data);
        $this->load->view('admin/includes/right',$this->$data);
        $this->load->view('admin/includes/footer',$this->$data);
    }
}

It fails at line 10 with these errors.

Code:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: admin/auth.php
Line Number: 10

Fatal error: Cannot access empty property in C:\Documents and Settings\gaw9a3\Desktop\aha 3.0\application\controllers\admin\auth.php on line 10
#6

[eluser]danmontgomery[/eluser]
$this->data, not $this->$data
#7

[eluser]Media Gearhead[/eluser]
I ask for help and I cannot even read things as written without getting it wrong... Kicking myself has already commenced and will continue for a few hours.

Thank you to all as all is working as I was hoping to get for the end result.




Theme © iAndrew 2016 - Forum software by © MyBB