• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Setting variables to be used controller wide.

#1
[eluser]uber_n00b[/eluser]
I want to setup a variable to be used by various functions in my controller. I have viewed one of the posts on here that offered a few suggestions but it was dealing with strings and not arrays.

How can I make $timezone_drop; available to all functions in my class/controller?

Code:
$timezone_drop = array(
       '' => 'Select Timezone',
        1 => 'Eastern',
        2 => 'Central',
        3 => 'Mountain',
        4 => 'Pacific',
        5 => 'Alaskan',
        6 => 'Hawaiian',
    );

I have tried placing it in my construct function
Code:
function __construct()
{
parent::__construct();

    $timezone_drop = array(
       '' => 'Select Timezone',
        1 => 'Eastern',
        2 => 'Central',
        3 => 'Mountain',
        4 => 'Pacific',
        5 => 'Alaskan',
        6 => 'Hawaiian',
    );
}
function index(){ $this->data['timezone'] = $timezone_drop; }

index() will throw an error saying $timezone_drop is undefined however if I place the variable declaration in index() it works fine.

Please help.

Thanks!

#2
[eluser]LuckyFella73[/eluser]
The contructor is the right place. Just write
Code:
$this->timezone_drop = array();

// instead of:
$timezone_drop = array();

In your methods use the same way:
Code:
$my_array = $this->timezone_drop;

#3
[eluser]uber_n00b[/eluser]
Thanks for the reply LuckyFella - I had tried that as well previously. This is the error I receive.


A PHP Error was encountered

Severity: Notice

Message: Undefined variable: timezone_drop

Filename: controllers/company.php

Line Number: 7

#4
[eluser]CroNiX[/eluser]
PHP likes its variables declared before using them.

Code:
class Something extends CI_Controller {

  //Declare the property
  public $timezone_drop = array();

  function __construct()
  {
    parent::__construct();

    $this->timezone_drop = array(
       '' => 'Select Timezone',
        1 => 'Eastern',
        2 => 'Central',
        3 => 'Mountain',
        4 => 'Pacific',
        5 => 'Alaskan',
        6 => 'Hawaiian',
    );
  }

  function index()
  {
    $this->data['timezone'] = $this->timezone_drop;  //although, its already available throughout your controller as $this->timezone_drop, so not sure why you are assigning it to another variable
  }
}

#5
[eluser]uber_n00b[/eluser]
Thank you for the help - it has been a long day and I made a stupid mistake - I was doing $this->$printer_drop as opposed to $this->printer_drop!

Thanks again.


#6
[eluser]uber_n00b[/eluser]
CroNIX - I assign it to another variable so I can pass $data[] to my view and use it to build a dropdown...Does that still not make sense?


#7
[eluser]CroNiX[/eluser]
You can just use $this->timezone_drop directly in your view without explicitly passing it, since it's a class property and globally available to anything from within that controller. No need for taking up extra memory within a separate variable.

#8
[eluser]InsiteFX[/eluser]
CroNiX showed you how to do it above
Code:
class Something extends CI_Controller {

  //Declare the property
  public $timezone_drop = array();

  function __construct()
  {
    parent::__construct();

    $this->timezone_drop = array(
       '' => 'Select Timezone',
        1 => 'Eastern',
        2 => 'Central',
        3 => 'Mountain',
        4 => 'Pacific',
        5 => 'Alaskan',
        6 => 'Hawaiian',
    );
  }

  function index()
  {
    $this->data['timezone'] = $this->timezone_drop;  //although, its already available throughout your controller as $this->timezone_drop, so not sure why you are assigning it to another variable

    // this will make it global to all your controllers and views. So now you do not need to pass it
    $this->load->vars($this->data);
  }
}

#9
[eluser]uber_n00b[/eluser]
I understand that InsiteFX.

CroNIX will give that a try in my view. Thanks again for the help.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.