4

I have a function in one of my views, and I want to access one of the variables available to the view through CodeIgniter's data array.

For example; in my controller I have this:

$this->load->view('someview', array(
    'info' => 'some info'
));

Now, within my view, I have a function, and I want to be able to access the variable $info from within that function scope.

Is that possible?

3
  • See no reason why not, it should be like using a helper. Commented Aug 8, 2011 at 14:51
  • @simnom: unlike Javascript, which has a scope lookup chain (If a variable is not found in the local scope, it looks for it higher up in the scope chain), PHP can only use local or global variables. Hence, the variables exposed to the view, are not available inside the function. Commented Aug 8, 2011 at 15:18
  • 2
    Then why not pass the variable to the function. You've got access to the $info just pass it to function_name($info). Commented Aug 8, 2011 at 15:36

3 Answers 3

4

in your controller:

    $GLOBALS['var_available_in_function'] = $value;

Your function:

    function get_var() {
       global $var_available_in_function;

       return $var_available_in_function;
    }
Sign up to request clarification or add additional context in comments.

Comments

3

I tried this but using globals in my view someview.php doesn't work..

function func_in_view(){
  global $info;
  print_r ($info); // NULL
}

You may need to pass this as a parameter instead to your function so it's available to it.

function func_in_view($info){
  print_r ($info); // NULL
}

I read this method $this->load->vars($array)
in http://codeigniter.com/user_guide/libraries/loader.html
but it's purpose is just to make it available to any view file from any function for that controller. I tried my code above global $info; and it still doesn't work.

You may need to do the workaround by passing it as a parameter instead.
Try including this in your someview.php => print "<pre>";print_r($GLOBALS);print "</pre>"; and the variables passed through $this->load->view aren't included.

1 Comment

You are right. I misread the post and have removed my answer. I suppose your answer is correct.
0

I solved this by creating a global variable within the view, then assigning the passed in $data value to the global variable. That way, the information can be read within the view's function without having to pass the variable to the function.

For example, in the controller, you have:

$data['variable1'] = "hello world";
$this->load->view('showpage',$data);

Then in the showpage.php view, you have:

global $local_variable = $variable1;

function view_function() {
  global $local_variable;
  echo $local_variable;
}

view_function();

Hope this helps!

2 Comments

The issue is, if you have many variables that are generate from an array for example the user's details and you want each single value to be in it's own variable to be accessed by the View
"global $local_variable = $variable1;" throws a syntax error here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.