Welcome Guest, Not a member yet? Register   Sign In
Declaring variable in view and the calling variable by using view
#1

[eluser]bleu[/eluser]
I am calling One view(a.php) from another view(b.php).
Can I declare a variable and assign a value to it in a.php and use it in b.php or do I have to declare it again in b.php.
#2

[eluser]davidbehler[/eluser]
View a.php:
Code:
<p>some text<p>
&lt;?php
  $x = "I'm awesome";
  $this->load->view('b', array('x' => $x));
?&gt;

View b.php
Code:
<p>&lt;?php echo $x; ?&gt;

Final output:
Code:
<p>some text<p>
<p>I'm awesome</p>
#3

[eluser]gRoberts[/eluser]
Or you could use include which removes the need to call $this->load->view?
#4

[eluser]bleu[/eluser]
[quote author="davidbehler" date="1333109582"]View a.php:
Code:
<p>some text<p>
&lt;?php
  $x = "I'm awesome";
  $this->load->view('b', array('x' => $x));
?&gt;

View b.php
Code:
<p>&lt;?php echo $x; ?&gt;

Final output:
Code:
<p>some text<p>
<p>I'm awesome</p>
[/quote]


Actually I want to call a.php from b.php and create a variable in a.php and then use it in b.php

b.php will call the the a view

in
Code:
b.php view  $this->load->view('a');

then create a variable in a.php and the use it in b.php

in
Code:
a.php $variable="123";


then in b.php

Code:
$variable=$variable+1;
#5

[eluser]davidbehler[/eluser]
Using $this->load->view() you can only pass variables in one direction, not back from the child view (a.php) to the parent view (b.php). I guess you'd have to go with include() or require() like gRoberts proposed above.
#6

[eluser]PhilTem[/eluser]
If you want to know more about the functionality provided by $this->load->view I'd suggest you read the source code since it helps understanding this crucial part of CI very well.

Just to mention some functions used by $this->load->view
Code:
extract();
ob_start();
ob_get_contents();

These methods actually prevent returning values from any view to what is executed after the view was loaded.

But you may do something like this

In the controller
Code:
function your_function()
{
  // do your stuff here

  // then create a variable of the class
  $this->data = array();
  
  // do whatever you want to do know
}

in view a.php
Code:
<p>some text<p>
&lt;?php
  $this->load->view('b');
?&gt;

in view b.php
Code:
<p>some text<p>
&lt;?php
  <p>&lt;?php $this->data['awesome_key'] = 'awesome value'; ?&gt;
?&gt;

From my understanding of CI, this should access the variable $this->data we defined further above in the controller. But I'm not sure and unable to test at the moment Wink
#7

[eluser]CroNiX[/eluser]
[quote author="davidbehler" date="1333376841"]Using $this->load->view() you can only pass variables in one direction, not back from the child view (a.php) to the parent view (b.php). I guess you'd have to go with include() or require() like gRoberts proposed above.[/quote]
Actually, you can use $this->load->vars($var_array) and have them accessible globally. If you do this, you don't need to explicitly pass the $var_array to the view, either.




Theme © iAndrew 2016 - Forum software by © MyBB