Welcome Guest, Not a member yet? Register   Sign In
variables from controler not appearing in view
#1

[eluser]Unknown[/eluser]
hello,

I don't know if somebody experienced this kind of problem.
I use the latest CI on debian squeeze (standard apache, php and xdebug packages) and I develop with eclipse (also standard debian squeeze package).

when I send variables from the controler to a view, some of them disapear. I mean, I traced the execution in loader.php and after the execution of the "extract()" which put the content of the array send from the controler to the view and which contains the variables. right after the "extract()", some variables aren't there.

after spent some time, I thought it was a problem with xdebug. but even if I remove xdebug, the problem is still there.
I tried to prefix the variables, put them in another array inside the variables array. still didn't work.

I corrected this by putting "$viewVariables = $this->_ci_cached_vars;" right after the "extract()" and before the include of the view. of course, I have to access my variables in the view using $viewVariables[ ] but all works fine now.

anybody had the same kind of problems ?

cordialement,
jp
#2

[eluser]jairoh_[/eluser]
if you want to send variables in your view from your controller put in an array,
ex.

controller:
<code>
$var [ 'name' ] = 'Kobe Bryant';
$this->load->view( 'nba', $var );
</code>

in your view( 'nba.php' ):
<code>
&lt;?php echo $name; ?&gt;
</code>
#3

[eluser]Unknown[/eluser]
yes, that's what I was doing. and it didn't work.
after the extract() is executed in the CI core function "load()", some variables which should have been copied from the array to the current scope are missing.
and I don't know why.

simple way:
$vars[ 'var1' ] = "content1";
$vars[ 'var2' ] = "content2";
$this->load->view( 'view_page', $vars );

view_page:
echo( $var1 );
echo( $var2 );


if I add some more variables, some are not sent to the view.
like in this this case, I could add:
$vars[ 'another_var' ] = array( 'test' => 'hope it works' );

and suddenly, echo( $var2 ); doesn't work anymore because $var2 is not present in the view.

#4

[eluser]boltsabre[/eluser]
Can you post the code of the controller that this is happening in?


It's not a simple mis-naming of your variables? Once I had something like this:

Code:
class Example extends CI_Controller{
   //inside my constructor, make a data variable
   $this->data['something'] = "boo"

   some_function(){
      //lots of code
      $this->data['another_var'] = 'something'
      //lots more code
      $this->load->view('someview', $this->data);
   }
}
And then I came back to the code a while later, chucked in a new $data variable, and it WASN'T on my view!?!?... because being the fool that I am, I'd called it:
$data['yet_another_var'] instead of $this->data['yet_another_var']

But I doubt this is your problem if you're seeing the missing variables with all the other variables just before exectution of “extract()"....
Apart from that, I've never personally experienced variables just going missing! Sorry I can't be of more help.
#5

[eluser]TheFuzzy0ne[/eluser]
That code will result in a parse error. Tongue

However, I agree. Please post your actual controller and view. What you've illustrated above should work. The fact that it's not, suggests that what you're doing and what you've illustrated that you're trying to do are not the same thing. If your code is sensitive, please crate a new method and view, and try to recreate the problem more simply.

From the example you've give, your PHP code isn't within PHP tags. Could that be the problem?




Theme © iAndrew 2016 - Forum software by © MyBB