Welcome Guest, Not a member yet? Register   Sign In
Setting a global variable in a controller?
#1

[eluser]Matrices[/eluser]
Hello fellow CI-ers!

I need to set a global variable within a controller that all the functions can access. The variable is $omit. The code is below. As you can see the $omit variable needs to be plugged into each function right now. This variable will be the same no matter what, so is there a way to set it globally, but only inside this specific controller?


Code:
<?php

class Admin extends Controller {

    function Admin()
    {
        parent::Controller();
        $this->load->scaffolding('categories');
    }

    function index()
    {
        $this->load->view('admin');
    }

    function categories()
    {
        // Give the view page a title
        $title = "Categories";
        
        // Query the table
        $query = $this->db->get('categories');
        
        // Get the table fields
        $fields = $this->db->list_fields('categories');
        $omit = array(
                         'ID',
                     );
        
        while (list ($key, $val) = each ($fields)) {
        
          if (in_array ($key, $omit)) unset ($fields[$key]);
        
        }
        
        // Place all the data into an array
        $data = array(
                        'query'    => $query,
                        'fields' => $fields,
                        'title'    => $title,
                     );
                    
        $this->load->view('admin', $data);
    }
    
    function articles()
    {
        // Give the view page a title
        $title = "Articles";
        
        // Query the table
        $query = $this->db->get('articles');
        
        // Get the table fields
        $fields = $this->db->list_fields('articles');
        $omit = array(
                         'ID',
                     );
        
        while (list ($key, $val) = each ($fields)) {
        
          if (in_array ($key, $omit)) unset ($fields[$key]);
        
        }
        
        // Place all the data into an array
        $data = array(
                        'query'    => $query,
                        'fields' => $fields,
                        'title'    =>$title
                     );
                    
        $this->load->view('admin', $data);
    }
    
}
?>
#2

[eluser]usmc[/eluser]
Like any object you can set properties.

Code:
$this->some_property = "Hello World";

Then access it from any method.
#3

[eluser]Bramme[/eluser]
Yup. This is basic OO PHP... I suggest you read up on it a bit, it's definately worht it!
#4

[eluser]Matrices[/eluser]
Ok I've replaced function Admin() with this code, and removed the $omit definitions from the individual functions:

Code:
function Admin()
    {
        parent::Controller();
        
        // Setting the fields which the view mode will ignore
        $this->omit = array(
                         'ID',
                           );
                            
        $this->load->scaffolding('articles');
    }

However I'm still receiving an error that omit is not defined.
#5

[eluser]Matrices[/eluser]
I got it, thanks for the help. I realized that I still need to define $omit inside the functions using

Code:
$omit = $this->omit;

I'm as green as sea glass, but I'm learnin'. Thanks for the help!
#6

[eluser]Hannes Nevalainen[/eluser]
You can do this to:
Code:
//Constructor
function Admin(){
  parent::Controller();
  
  //Variables defined here will be passed to all your views!
  $this->load->vars(array(
    'ID'
  ));
}

EDIT:
Corrected the comment ('views' instead of 'controllers')
#7

[eluser]xwero[/eluser]
First of all if you want to add a class variable you should define it in the top of your class
Code:
class somename
{

   var $omit;
}
The var keyword is the same as public in php5 but in php5 you can limit the scope of the variable with the protected and private keywords.

When you define the variable it's possible to add a default value like you do with normal values.

Matrices in your methods you can use $this->omit in place of the $omit variable name.

Hannes the load->vars method is meant to collect variables for the view files not as a substitute for class variables.
#8

[eluser]Hannes Nevalainen[/eluser]
xwero > I know, but he sends it to the view in every controller so I don't think it's such an bad idea.

I wrote something wrong in my post, the comment in the code should say 'views' instead of 'controllers'
#9

[eluser]srobet[/eluser]
[quote author="xwero" date="1218194148"]

When you define the variable it's possible to add a default value like you do with normal values.

Matrices in your methods you can use $this->omit in place of the $omit variable name.

Hannes the load->vars method is meant to collect variables for the view files not as a substitute for class variables.[/quote]

Yups, I agree with that
#10

[eluser]xwero[/eluser]
Hannes i think you are looking at the wrong code Wink

Code:
// Get the table fields
        $fields = $this->db->list_fields('categories');
        $omit = array(
                         'ID',
                     );
        
        while (list ($key, $val) = each ($fields)) {
        
          if (in_array ($key, $omit)) unset ($fields[$key]);
        
        }
He uses it to remove fields he got from the db->list_fields method.




Theme © iAndrew 2016 - Forum software by © MyBB