Welcome Guest, Not a member yet? Register   Sign In
Helpers access $this->load->vars($data) ? [take-2]
#1

[eluser]stormbytes[/eluser]
I originally posted this last night & thought the matter resolved. Trying to pass a variable to a Helper through a call just won't work. This is form input so it involves set_value() & validation, etc.

So I'm back to my original question -


I’m writing some helper functions which are called from within a view, and I’m wondering if there’s a way to write the helper function so that it can access variables loaded (from within the originating controller) through $this->load->vars($data). Perhaps some $CI method I don't know of, or some hack/workaround.


Thanks!
#2

[eluser]tonanbarbarian[/eluser]
i do not know exactly if what you are trying to do is possible, but i do know what i would do to try and see if it is

in your helper grab an instance of the controller and then do a print_r of the controller and see if you can find the data you are looking for somewhere
Code:
function my_helper() {
  $CI =& get_instance();
  echo '<pre>'.print_r($CI,true).'</pre>';
}
if nothing else you could always store the data you want to be able to access as another property of the controller before you pass that data to the view and then access it as needed
Code:
$CI->_my_data['variable']
#3

[eluser]InsiteFX[/eluser]
Returning views as data
There is a third optional parameter lets you change the behavior of the function so that
it returns data as a string rather than sending it to your browser. This can be useful if
you want to process the data in some way. If you set the parameter to true (boolean) it
will return data. The default behavior is false, which sends it to your browser. Remember
to assign it to a variable if you want the data returned:

Code:
$string = $this->load->view('myfile', '', true);

CodeIgniter Views

InsiteFX
#4

[eluser]stormbytes[/eluser]
[quote author="wiredesignz" date="1289112749"]
Code:
$this->_ci_cached_vars; //array used by $this->load->vars() referenced from within a view.
[/quote]

I've tried the obvious but it seems I'm missing something..

Controller:
Code:
$edit = array('name' =>'john', 'id'=>'5');
$data['edit'] = $edit;
$this->load->vars($data);

Helper Function:
Code:
$CI =& get_instances();
$foo = $CI->_ci_cached_vars;
echo $foo['edit']['name'] // Does not produce 'John'.
#5

[eluser]stormbytes[/eluser]
left out the 'load' method/stmt.

$CI->load->_ci_cached_vars works like a charm!

Thanks people
#6

[eluser]dudeami0[/eluser]
The way wiredesignz is talking about is for accessing it inside the view, and calling the helper function from there like:

Code:
&lt;html&gt;
&lt;head&gt;
...
&lt;/head&gt;
&lt;body&gt;
&lt;?php echo my_helper_function($this->_ci_cached_vars); ?&gt;
&lt;/body&gt;
&lt;/html&gt;

The way your going about could be done (With $CI->load->_ci_cached_vars):
Code:
$CI =& get_instances();
$foo = $CI->load->_ci_cached_vars;
echo $foo['edit']['name'] // Does not produce 'John'.

Either way should work Smile

Edit: Didn't notice your reply, I'll just leave this up here Smile
#7

[eluser]dudeami0[/eluser]
For a way to do this with keeping from the helper referencing the controller class is just to pass on the vars that are cached in the array. Something like this would work:

Code:
function myControllerFunction() {
    $this->load->vars(Array('varname' => 'varvalue'));
    my_helper_function($this->load->_ci_cached_vars);
}

Code:
function my_helper_function($vars) {
    $varname = $vars['varname'];
}

and if you want variables, use the php extract function:

Code:
function my_helper_function($vars) {
    extract($vars);
    return $varname;
}

Edit: $this->load->_ci_cached_vars is actually the way from the controller
#8

[eluser]wiredesignz[/eluser]
Code:
$this->_ci_cached_vars; //array used by $this->load->vars() referenced from within a view.




Theme © iAndrew 2016 - Forum software by © MyBB