CodeIgniter: how to pass values from a specific function to another one

怎甘沉沦 提交于 2019-12-23 03:45:09

问题


I am trying to pass a value coming from a form using the method post from a function to another one.

class Send_value extends CI_Controller {

public function main_page() {
     $data['user'] = $this->input->post('fullName');
      ...
}


public function welcome_page(){
  //Now I would like to pass $data['user'] here.      

}
}

How can I pass it in the second function?


回答1:


You need to pass parameter within that function and need to define that parameter as

class Send_value extends CI_Controller {

public function main_page() {
     $data['user'] = $this->input->post('fullName');
     echo $data['user']; //echoing value from post
     if(!empty($data)){
        $this->welcome_page($data); //passing data into another function
     } else {
        echo "Didn't get value from post";
     }
}


public function welcome_page($data = ''){
                           //^^ parameter set within function
    if(is_array($data) && count($data) > 0){
       print_r($data);
    } else {
       echo "No result found";
    }
  }
}



回答2:


You need to have your other method accept a parameter and then you can pass $data to it.

class Send_value extends CI_Controller {

public function main_page() {
     $data['user'] = $this->input->post('fullName');
    $this -> welcome_page($data);
}


public function welcome_page($data){
  //Now I would like to pass $data['user'] here.      
    echo $data['user'];
}
}



回答3:


public function main_page() {
     $data['user'] = $this->input->post('fullName');
      $this->welcome_page($data['user']);

... }

public function welcome_page($zz){
  //Now I would like to pass $data['user'] here.
  //$zz contains your post data

}


来源:https://stackoverflow.com/questions/29612324/codeigniter-how-to-pass-values-from-a-specific-function-to-another-one

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!