Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have just started learning Code Igniter .

I want to know how can I pass a variable from one controller(first_cont.php) to other controller (second_cont.php) ?

Any help would be appreciated .

Thanks in Advance :)

like image 714
WomenInTech Avatar asked Oct 10 '12 09:10

WomenInTech


2 Answers

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.

like image 182
Matthew Daly Avatar answered Oct 12 '22 09:10

Matthew Daly


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

like image 33
Kalpesh Patel Avatar answered Oct 12 '22 10:10

Kalpesh Patel