0

I want to assign the value of $Id from firstFunction to the variable $temp in secondFunction.

public function firstFunction()
{
    $Id = $_POST['quizID_click'];
}


public function secondFunction()
{
    $temp = ------;
}

Please let me know how to access $Id in secondFunction

1
  • 3
    create it as a property of the class Commented Nov 13, 2015 at 9:10

5 Answers 5

2

To expand on my comment, you could create your variable as a property of your class.. it's been a while since used CI so forgive me, classes are classes though :)

class whatever
{
    public $id;

    public function firstFunction($postData)
    {
         $this->id = $postData['quizID_click'];
    }

    public function secondFunction()
    {
        $temp = $this->id;
    }
}

Hope that gets you going

Sign up to request clarification or add additional context in comments.

Comments

1

If i understand correctly you want to pass only a variable inside the Controller functions, You can do it by throwing a flash like this:

    public function firstFunction()
    {
        $this->load->library('session');
        $Id = $_POST['quizID_click'];
        $this->session->set_flashdata('item', $Id);
    }


    public function secondFunction()
    {
        $myVar = $this->session->flashdata('item');
    }

Basically you temporary save the variable value as flashdata giving it a 'item' value 'item' can be anything. then the second controller looks for this flashdata and asigns it to $myVar

3 Comments

Why are you using sessions for this? He could simply call the 2nd function from the first with a param, like you'd always do? What is the benefit of doing it through a session?
@killstreet the second method might be a big and doesn't return only one variable, u cannot access a variable unless u return it with a function, but a function can not always be used to return one variable
In cases you need to return multiple variables you would just create an array
1

Functions create local variables which are destroyed when the function completed. To maintain the variable you need, you'll have to return it from the function and then assign it to a variable.

I can see that you are using classes, so you'll need to access the function like a method. This should work:

public function firstFunction()
{
    $Id = $_POST['quizID_click'];
    return $Id;
}


public function secondFunction()
{
    $temp = $this->firstFunction();
}

Comments

1

Create a public variables of your class so that you can access the variables in all methods.

class myClass{
    public $id;

    public function first(){
        $this->id = $this->input->post('quizID_click');
    }

    public function second(){
        $id = $this->id;
    }
}

But that depends on what you are tying to accomplish? Please elaborate what you are trying to do.

Comments

0
public function firstFunction()
{
    $Id = $_POST['quizID_click'];
    $this->seconFunction($id);
}


 public function secondFunction($id)
 {
     $temp = $id;
 }

This is how to pass variables among functions.

Comments

Your Answer

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