I am developing a web app using codeigniter. N开发者_StackOverflowow if I want to load variables from one function to another function when the function is called, then is it possible? Consider an example below:
Supppose there are two functions inside a same class as follows:
Class Form extends CI_Controller(){
function submit()
{
$data['title'] = "Website";
$data['header'] = "Welcome to my website";
$this->load->vars($data);
$this->load->view('welcome');
}
function xyz()
{
$data['footer'] ="Mywebsite 2011 Copyright";
$this->submit();
}
}
Is it possible that when I call xyz function, the $data['footer'] is included in the submit() function? Like submit() loads $data['footer'] into $this->loads->vars($data)? If not what can be done to include the same?
There are two ways to do this. The first one is to pass the data as a parameter, for instance:
Class Form extends CI_Controller(){
function submit($footer = false)
{
$data['footer'] = $footer; // set the footer.
$data['title'] = "Website";
$data['header'] = "Welcome to my website";
$this->load->vars($data);
$this->load->view('welcome');
}
function xyz()
{
$data['footer'] ="Mywebsite 2011 Copyright";
$this->submit($data['footer']);
}
}
The alternative is to set the footer data as a class variable, which would be available to all functions in the controller, like this:
Class Form extends CI_Controller(){
var $footer = false;
function submit()
{
$data['footer'] = $this->footer; // get the footer.
$data['title'] = "Website";
$data['header'] = "Welcome to my website";
$this->load->vars($data);
$this->load->view('welcome');
}
function xyz()
{
$this->footer = "Mywebsite 2011 Copyright";
$this->submit($data['footer']);
}
}
精彩评论