我正在尝试在我的网站上放置一个动态标题,所以就在这里.
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');
}
}
首先触发的是validate_stub函数,然后我想将get_quest_title的返回值传递给全局变量$my_title,然后将其传递给survey_form函数.在这种情况下,$this-> my_title为空白,我如何从db传递标题,然后将其放入全局变量,然后传递至视图.谢谢
解决方法:
我不明白为什么您在validate_stub()中使用重定向.您可以直接调用survey_form函数,这里是代码:
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');
}
}