ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Declaring variable in view and the calling variable by using view

March 30, 2012 7:01am

Subscribe [4]
  • #1 / Mar 30, 2012 7:01am

    bleu

    36 posts

    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 / Mar 30, 2012 8:13am

    davidbehler

    708 posts

    View a.php:

    some text
    <?php
      $x = "I'm awesome";
      $this->load->view('b', array('x' => $x));
    ?>

    View b.php

    <?php echo $x; ?>

    Final output:

    some text
    I'm awesome
  • #3 / Mar 30, 2012 8:58am

    gRoberts

    298 posts

    Or you could use include which removes the need to call $this->load->view?

  • #4 / Apr 02, 2012 10:24am

    bleu

    36 posts

    View a.php:

    some text
    <?php
      $x = "I'm awesome";
      $this->load->view('b', array('x' => $x));
    ?>

    View b.php

    <?php echo $x; ?>

    Final output:

    some text
    I'm awesome


    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

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

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

    in

    a.php $variable="123";


    then in b.php

    $variable=$variable+1;

     

  • #5 / Apr 02, 2012 10:27am

    davidbehler

    708 posts

    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 / Apr 02, 2012 10:56am

    PhilTem

    872 posts

    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

    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

    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

    some text
    <?php
      $this->load->view('b');
    ?>

    in view b.php

    some text
    <?php
      <?php $this->data['awesome_key'] = 'awesome value'; ?> 
    ?>

    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 😉

  • #7 / Apr 02, 2012 1:28pm

    CroNiX

    4713 posts

    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.

    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.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases