3

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. 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. Thanks

1 Answer 1

3

I can't understand why you use redirect in validate_stub(). You can call the survey_form function straight here 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');
   }
}
Sign up to request clarification or add additional context in comments.

6 Comments

thanks! btw coz it's a form action if i put $this->survey_form() the link will be /validate_stub, i want it as /survey_form
So what's the use of validate_stub() ? You can copy/paste the code from validate_stub() to survey_form() and use that function instead. Or you should consider using form_validation codeigniter.com/userguide3/libraries/form_validation.html
i have a long validation logic there that's why i separated the validation
In order not to change your logic, you can pass the title through session. Check edit.
O my god i didn't think that gosh Thanks mate!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.