CodeIgniter:如何将值从特定函数传递到另一个函数

时间:2015-04-13 18:20:06

标签: php codeigniter

我正在尝试使用方法post将函数中的值传递给另一个函数。

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.      

}
}

如何在第二个函数中传递它?

3 个答案:

答案 0 :(得分:1)

您需要让其他方法接受参数,然后才能将$ data传递给它。

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'];
}
}

答案 1 :(得分:1)

您需要在该函数中传递参数,并需要将该参数定义为

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 :(得分:0)

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

}