2

I'm quite new to CodeIgniter and I'm trying to re-use the $data I pass to a view in another function from the same controller.

I have the following code :

class MyClass extends CI_Controller
{
    function func1()
    {
        $this->mdata['first'] = "first";
        $this->mdata['second'] = "second";  
        $this->load->view('my_view', $this->mdata);
    }

    function func2()
    {
        var_dump($this->mdata);
    }
}

The fact is that apparently, I can't use my variable in the func2()...

Does someone has a trick to do so ?

Thanks.

B

4 Answers 4

2

Create a private method _create_mdata() and call it in both methods. There is no way to literally share the data without doing something like this.

// methods starting with an underscore are considered private by CodeIgniter.
// you may want to actually declare it private though. That is better practice
function _create_mdata() 
{
    $this->mdata['first'] = "first";
    $this->mdata['second'] = "second";  
}

function func1()
{
    $this->_create_mdata(); 
    // continue with func1
}
Sign up to request clarification or add additional context in comments.

1 Comment

You can also put stuff in application/config/constants.php
0
class Your_Class {
    public $mdata;

    function func1()
    {
        $this->mdata['first'] = "first";
        $this->mdata['second'] = "second";  
        $this->load->view('my_view', $this->mdata);
    }

    function func2()
    {
        var_dump($this->mdata);
    }
}

Now "$this->mdata" will work.

Hope it helps.

3 Comments

It doesn't :( The "var_dump($this->mdata)" returns me NULL
If func2 is called in another URL request it will not be initiallized from the func1 call. I guess the data will have to be saved in a session.
The func2 is called after a form validation.
0

func1 is only called when the user browser to yoursite.com/myclass/func1. Now, going to func2 using the url only calls func2. Therefore, the mdata values are not assigned. You could use a constructor where you assign those values. I'll put up a code example when I have more time.

Comments

0

Pseudo code i haven't tested. And you probably should autoload session.

   class Your_Class {

        function func1()
        {
            $this->load->library('session');

            $mdata = array( 'mdata' => array(   'first' => 'first',
                                                'second' => 'second'
                                            )
                         )  
            $this->session->set_userdata($mdata);

            $this->load->view('my_view', $mdata['mdata']);
        }

        function func2()
        {
            $this->load->library('session');
            $mdata = $this->session->userdata('mdata');
            var_dump($mdata);
        }
    }

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.