问题
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