2

I have 2 functions/methods in the same controller in CodeIgniter like this:

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);

EDIT: SOLVED!

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

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.:

<?php echo $this->ps->page_name; ?>

1 Answer 1

2

$data simply has different visibility scope, this is not CodeIgniter specific. You can solve this in two ways:

  • Make $data a class property ($this->data)
  • Make _get_page_settings() to return $data and assign it to a variable when calling it.
Sign up to request clarification or add additional context in comments.

1 Comment

Can you be more specific, please? ;)

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.