问题
I've tried a lot but I am not getting my requirement. I have a controller in which I declared a private var and assigning this in one of the function of the controller. Then I want to use it in another function when called from a link directly that function.
class Xyz extends CI_Controller {
private $var;
public function __construct()
{
parent::__construct();
$this->var = null;
}
public function getGenVal(){
$this->var = $_REQUEST['value']; //Setting the global variable
}
//Calling bellow function directly from a link
public function globalVarValue(){
$val = $this->var;
echo $val; //Nothing displayed.
}
}
回答1:
class Xyz extends CI_Controller {
private $var;
public function __construct()
{
parent::__construct();
$this->var = null;
}
public function getGenVal(){
$this->session->set_userdata('val',$_REQUEST['value']); //Setting the global variable
}
//Calling bellow function directly from a link
public function globalVarValue(){
if($this->session->userdata('val'))
{
echo $this->session->userdata('val');
}
}
}
来源:https://stackoverflow.com/questions/45144768/set-a-variable-value-in-one-function-and-use-in-other-functions-when-called-dir