Set a variable value in one function and use in other functions, when called directly from link in codeigniter

扶醉桌前 提交于 2019-12-25 10:02:22

问题


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

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