A controller often comprises of several functions and often the module’s logic demands passing data between the various functions of the Controller. This is a common enough scenario in many Codeigniter projects. In this tutorial, I will demonstrate how you could pass data between two functions within the same Controller in CodeIgniter. In addition, I will show this data in the View.
Here is what the Model looks like:
Model
function getResponse($gettingresponse) { $enrollresponse=$gettingresponse['sendresponse']; return $enrollresponse; }
The Controller is as follows:
Controller
public function Register() { Â Â Â $this->load->view('firstview'); Â Â Â $this->load->view('secondview'); Â Â Â if($_POST) { Â Â Â Â Â Â Â $gettingresponse=array( Â Â Â Â Â Â Â Â Â Â Â 'sendresponse'=>$_POST['source'], Â Â Â Â Â Â Â Â Â Â Â 'receiverresponse'=>$_POST['destination'] Â Â Â Â Â Â Â ); Â Â Â Â Â Â Â $registration_confirm=$this->systemModel->responselogin($gettingresponse); Â Â Â Â Â Â Â $resposeflag=$this->systemModel->getEmail($gettingresponse); Â Â Â Â Â Â Â $data['resposeflag']=$gettingresponsevalue; Â Â Â Â Â Â Â if($registration_confirm){ Â Â Â Â Â Â Â Â Â Â Â $this->token($data); Â Â Â Â Â Â Â } Â Â Â } Â Â Â $this->load->view('thirdview'); } public function token($data=array()) { Â Â Â $this->load->view('firstview'); Â Â Â $data['resposeflag']; Â Â Â $this->load->view('token',$data); Â Â Â $this->load->view('thirdview'); }
The following View shows the data that has been passed between the functions of the Controller.
You might be interested in:Â How To Pass Data From Controller To View In CodeIgniter
View
<?php echo form_input(array('name'=>'source','readonly'=>'true','value'=>$resposeflag)); ?>
Stop Wasting Time on Servers
Cloudways handle server management for you so you can focus on creating great apps and keeping your clients happy.
Conclusion
In this tutorial, I have discussed how to pass data from one function to another in a Controller. You could easily extend this idea within your Codeigniter projects built on any PHP Hosting. If you need help, do leave a comment below.
Shahroze Nawaz
Shahroze is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. Besides his work life, he loves movies and travelling.