how to pass a variable from one controller to the other in Code igniter

It will depend on the circumstances. If you want to retain the data for some time, then session data would be the way to go. However, if you only need to use it once, flash data might be more appropriate.

First step would be to initialise the session library:

$this->load->library('session');

Then store the information in flash data:

$this->session->set_flashdata('item', $myVar);

Finally, in the second controller, fetch the data:

$myVar = $this->session->flashdata('item');

Obviously this would mean you'd have to either initialise the session library again from the second controller, or create your own base controller that loads the session library and have both of your controllers inherit from that one.


I think in codeigniter you can't pass variable, between two different controller. One obvious mechanism is to use session data.


Ok, here is something about MVC most will readily quote:

A Controller is for taking input, a model is for your logic, and, a view is for displaying.

Now, strictly speaking you shouldn't want to send data from a controller to another. I can't think of any cases where that is required.


But, if it is absolutely needed, then you could simply use redirect to just redirect to the other controller.

Something like:

// some first_cont.php code here
redirect('/second_cont/valuereciever/value1')


// some second_cont.php code here
public function valureciever($value){
    echo $value; // will output value1
}