简体   繁体   English

Codeigniter控制器中的动态全局变量

[英]Dynamic global variable in Codeigniter Controller

I'm trying to put a dynamic title on my website so 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 trigger is the validate_stub function then i would like to pass the return of get_quest_title to global variable $my_title then pass it to the survey_form function. 第一个触发的是validate_stub函数,然后我想将get_quest_title的返回值传递给全局变量$ my_title,然后将其传递给survey_form函数。 In this case $this->my_title is blank, how can i pass the title from db then put into the global variable then pass to the view. 在这种情况下,$ this-> my_title为空,我如何从db传递标题然后放入全局变量然后传递给视图。 Thanks 谢谢

I can't understand why you use redirect in validate_stub() . 我无法理解你为什么在validate_stub()使用redirect You can call the survey_form function straight here is the code: 你可以在这里直接调用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');
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM