Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] [1.71] Passing data
#1

[eluser]Jeroen Brussich[/eluser]
Hello,

I'm new @ CI and I'm already loving it.
Being new though, I struggle with some basic CI concepts (I think).

Let's say I have a dummy class.
Code:
class Dummy Extends Controller {
  
   function __construct()
   {
        Parent::Controller();
        $this->data['foo'] = 'bar';
   }
  
   function index()
   {
        print $this->data['foo'];        //works
   }
  
   function doStuff()
   {
        print $this->data['foo']         //works
        
        $this->data['number'] = 1;
        print $this->data['number'];     //works
   }
  
   function doSomethingElse()
   {
        print $this->data['foo']         //works
        print $this->data['number'];     //fails
   }
}

For some reason I can print $this->data['foo'] in all functions but $this->data['number'] only in the function I declared it.

Is there a way to make $this->data['number'] globally available in all my functions, just as $this->data['foo'] is, without declaring it in the __construct?

Thanks in advance.
#2

[eluser]WanWizard[/eluser]
You should define your class variables, like:
Code:
class Dummy Extends Controller {

   // define my class variable
   var data = array();

   function __construct()
   {
        Parent::Controller();
        $this->data['foo'] = 'bar';
   }
#3

[eluser]Jeroen Brussich[/eluser]
Really?

That easy???

Ok, guess my className was not that bad chosen :p

thanks!

---------------------
edit
---------------------


I was too early to cry victory.
It still doesn't work.

I guess I have to use sessions to pass data from one function to another.

Code:
class Dummy Extends Controller {
  
   var $data            = array();
  
   function __construct()
   {
        parent::Controller();
        $this->data['foo'] = 'bar';
   }
  
   function index()
   {
        print $this->data['foo'];        //works
   }
  
   function doStuff()
   {
        print $this->data['foo'];         //works
        
        $this->data['number'] = 1;
        print $this->data['number'];      //works
   }
  
   function doSomethingElse($data == $this->data)
   {
        print $this->data['foo'];         //works
        print $this->data['number'];      //fails
   }
}
#4

[eluser]lordmontie[/eluser]
The $data class var is loaded each time the controller is loaded and the single function is called. If you need to pass data between controller loads, you'll have to either use sessions or function parameters or POST form data. Because $this->data['foo'] is set in the controller's construct, it's set for all loads of controller while $this->data['number'] is set only in the doStuff function.
#5

[eluser]John_Betong[/eluser]
 
Maybe try this line of code in each function and get an indication of what is being loaded automatically.
Code:
echo __FUNCTION__;
 
 
 
#6

[eluser]Jeroen Brussich[/eluser]
[quote author="lordmontie" date="1251927898"]The $data class var is loaded each time the controller is loaded and the single function is called. If you need to pass data between controller loads, you'll have to either use sessions or function parameters or POST form data. Because $this->data['foo'] is set in the controller's construct, it's set for all loads of controller while $this->data['number'] is set only in the doStuff function.[/quote]

I thought as much.
Thanks for the clarification, though.

Being new @ CI, I wasn't sure session (or flashdata in my case) was the correct way to go.
#7

[eluser]Terry Valladon[/eluser]
If you are only going to use the data in a view you can also call $this->load->vars($array) in your constructor.

Quote:This function takes an associative array as input and generates variables using the PHP extract function. This function produces the same result as using the second parameter of the $this->load->view() function above. The reason you might want to use this function independently is if you would like to set some global variables in the constructor of your controller and have them become available in any view file loaded from any function. You can have multiple calls to this function. The data get cached and merged into one array for conversion to variables.

From how you are making use of it ($this->data rather then $this->some_random_var) I am assuming that this is more along the lines of what you want.

I know you said this was SOLVED but wanted to add this in for others who might come across the thread in a search.




Theme © iAndrew 2016 - Forum software by © MyBB