zero
zero

Reputation: 1

Codeigniter pass variable between functions

How can i pass variable from function a to b in codeigniter controller.

function a()
{

   $id = $this->input->post('id');

}

I want $id from function a to be passing to b

function b() 
{

    if($id) 
    {
       .....

    }

}

How can i do that?

Upvotes: 0

Views: 1914

Answers (3)

zero
zero

Reputation: 1

i used $this->session->set_userdata('a', $a); to pass the variable and it can be accessed by all method within the controller. to access it i used $b = $this->session->userdata('a');

thanks for all the help :D

Upvotes: 0

Vin
Vin

Reputation: 95

create $id

public $id = false;

then use

function a(){
    $this->id = $this->input->post('id');
}

function b(){
    if($this->id){.....}
}

in controls class

class TestApi extends MY_Controller {
        public function __construct() {
            parent::__construct();
        }

        public t1 = false;

        public function a () {
            $this->t1 = true;
        }

        public function b () {
            a();
            if($this->t1){
                ...
            }
        }
}

or try global var? but not a good idea in framework

$a1 = 5;
function Test()
{
    $GLOBALS['a1'] = 1;
}

Test();
echo $a1;

In the Same Class? maybe should check var or what u get from input?

Class Test{
    public $t = 1;

    public function a(){
        $this->t = 20;
    }

    public function b(){
        echo $this->t;
    }

}

$TestClass = new Test;

echo $TestClass->t;
echo "-";
echo $TestClass->a();
echo ">";
echo $TestClass->t;

Upvotes: 2

Monico Sabala
Monico Sabala

Reputation: 1

function a(){
    $id = $this->input->post('id');
    $this->b($id);

}

function b($id){
    if ($id) {
        //....
    } else {
        //.....
    }
}

Upvotes: 0

Related Questions