Welcome Guest, Not a member yet? Register   Sign In
Is $data persistent?
#1

[eluser]stevek42[/eluser]
You know those problems you can't really find that really tug at your brain?

I have a method that is calling one view multiple times, passing in different data each time (it's a modular 'block' view that is part of a larger view). I have ensured beyond all doubt that $data is fresh and different each time it goes to the view. But if the view is called and the variable is undefined, it actually 'becomes' defined and populated with the last value!

For instance:

Code:
1.  Call once, $data['whatever'] = 'Hello';
<?=$whatever?> displays: Hello

2.  Call two, $data['whatever'] = 'Hi';
<?=$whatever?> displays: Hi

3.  Call three, if(isset($data)) unset($data);
<?=$whatever?> displays: Hi

4.  Call four, if(isset($data)) unset($data);
<?=$whatever?> displays: Hi

Does $data get stored anywhere else, like in session data? I am using FAL if that makes a difference. I am at my wit's end right now, I'm hoping someone has come across something like this.
#2

[eluser]gtech[/eluser]
yes you are correct there is somthing storing the variable $whatever, the CI loader class will cache the view variables within the page request..

I just took a look at the code and found a quick hack for you, the $data array is irrelevant you can pass an array called $vegetables into $this->load->view('view',$vegetables).

Code:
<?php
class Home extends Controller   {
  function Home(){
    parent::Controller();
  }


  function index() {

    $an_array['whatever'] = 'Hello';
    $this->load->view('testview', $an_array);
    $an_array['whatever'] = 'Hi';
    $this->load->view('testview', $an_array);

    // this will remove $whatever in the cached vars array.. dont use unset!
    // with out this call $whatever will still = 'Hi' inside the view
    $this->load->_ci_cached_vars = array();
    $this->load->view('testview');
  }
}
?>
testview.php
Code:
<hr>
&lt;?=$whatever?&gt;
<hr>
#3

[eluser]stevek42[/eluser]
I tried using the line that sets the cached_vars to a blank array, it still carried on $data. However, thanks for the tip about being able to pass whatever arrays I want, that's excellent. I'll just pass a different array name based upon the module I'm calling.

Thanks very much!
#4

[eluser]gtech[/eluser]
$data is a red herring!
just so you get an understanding of what I did:

If you cut and copied my controller and view code exactly you will see that the 3rd load view will error as $whatever has been unset by clearing the _ci_cached_vars.

If you commented that line out however, $whatever will still be available to to the 3rd view even though I do not pass a 'data' array to the 3rd load view.

Infact even if you pass a 'data' array called 'fish' with totally different variable elements
eg. $fish['newvar'] to the 3rd view the $whatever will STILL be available in the view. you need to clear the _ci_cached_vars array if you want $whatever to be unavailable in the view.
#5

[eluser]stevek42[/eluser]
Ahhh, I see what you're saying. Excellent, thank you very much for the explanation.




Theme © iAndrew 2016 - Forum software by © MyBB