Dynamic global variable in Codeigniter Controller
I am trying to put a dynamic header on my website, here it is.
class Survey extends MY_Controller {
public $my_title;
public function __construct(){
parent::__construct();
$this->load->model('Survey_model');
$this->my_title = ""; //setting to blank
}
public function survey_form(){
$this->data['title'] = $this->my_title; //display the title
$this->middle = 'Survey_view';
$this->layout();
}
public function validate_stub($survey_code){
$data = $this->Survey_model->get_questions($survey_code);
$this->my_title = $this->Survey_model->get_quest_title($survey_code); //getting from database title
$this->session->set_userdata('stub_data', $data);
redirect('Survey/survey_form');
}
}
The first to run is the validate_stub function, then I would like to pass return get_quest_title to the global $ my_title variable and then pass it to the survey_form function. In this case, $ this-> my_title is empty, how can I pass the title from db and then put into a global variable and then go to the view. Thanks to
+3
source to share
1 answer
I don't understand why you are using redirect
in validate_stub()
. You can call the function survey_form
right here, this is the code:
class Survey extends MY_Controller {
public $my_title;
public function __construct(){
parent::__construct();
$this->load->model('Survey_model');
$this->my_title = ""; //setting to blank
}
public function survey_form(){
$grab_title = $this->session->userdata('my_title');
if(isset($grab_title) && $grab_title != "") {
$this->data['title'] = $grab_title;
}else {
//do some checks here and add something default
$this->data['title'] = $this->my_title;
}
$this->middle = 'Survey_view';
$this->layout();
}
public function validate_stub($survey_code){
$data = $this->Survey_model->get_questions($survey_code);
$this->my_title = $this->Survey_model->get_quest_title($survey_code); //getting from database title
$this->session->set_userdata('stub_data', $data);
$this->session->set_userdata('my_title', $this->my_title);
redirect('Survey/survey_form');
}
}
+3
source to share