0

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?

3 Answers 3

2

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;
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for respon, the code works but why i dont get value in variable id?, it keep returning null value..
function a, b in the same class ?
when i use var_dump on post, it give the value from input text, when i tried to pass it as you suggested it return null
can you print $this->id and $this->input->post('id') in function a?
do i have to print it in view? if yes, how to print it? sory i'm still learning by tiral and error for this. bdw thanks for respond
|
0
function a(){
    $id = $this->input->post('id');
    $this->b($id);

}

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

1 Comment

Thanks for respon, i tried that but it give me this error Severity: Warning Message: Missing argument 1, it probably because i call function b from iframe in view, maybe this error happend because when i call this function from view i dont provide parameter id to be passed...
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.