Welcome Guest, Not a member yet? Register   Sign In
How to send a variable from one method into another within the same controller?
#1

[eluser]term25[/eluser]
I have 2 functions/methods in the same controller in CodeIgniter like this:
Code:
public function _get_page_settings() {
     $page_settings = $this->page_model->get_page_settings();
     $data['ps'] = $page_settings;
}
    
public function registration() {
     $this->_get_page_settings();
     $this->load->view('page_registration', $data);
}

The first method _get_page_settings() grabs some stuff from db and store it in a variable ps inside data array.

I want to use this method in many other methods inside this controller.

So, how to send data correctly to registration() because this code above is not working OK. It throw this error in my view:
Message: Undefined variable: data on line 70 which is this line:
$this->load->view('page_registration', $data);
#2

[eluser]term25[/eluser]
EDIT: SOLVED!

I have finally figured it out. You can do it like this:

Code:
public function _get_page_settings() {
    $data['ps'] = $this->page_model->get_page_settings();
    $this->ps = $data['ps'];
}

public function registration() {
    $this->_get_page_settings();
    $this->load->view('page_registration', $this->ps);
}

And to output it in the view e.g.:

Code:
<?php echo $this->ps->page_name; ?>
#3

[eluser]porquero[/eluser]
You can use Flashdata too:

http://ellislab.com/codeigniter/user-gui...sions.html
#4

[eluser]skunkbad[/eluser]
You can also use class members (variables) to hold data that can be shared between methods, and even another class, provided it has the right visibility.




Theme © iAndrew 2016 - Forum software by © MyBB