Mahantesh
Mahantesh

Reputation: 357

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

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.
            }
        }

Upvotes: 0

Views: 53

Answers (1)

hrishi
hrishi

Reputation: 1656

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

        }
    }

Upvotes: 1

Related Questions