我正在尝试在我的网站上添加动态标题,所以就在这里。

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 传递标题然后放入全局变量然后传递给 View 。谢谢

最佳答案

我不明白你为什么在 redirect 中使用 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');
   }
}

关于php - Codeigniter Controller 中的动态全局变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44017602/

10-12 17:43