CyberJunkie
CyberJunkie

Reputation: 22674

Using a Model function in Controller

In my model named Profile_model I have this function which retrieves profile data for the logged in user.

function profile_read()
{
    $this->db->where('user_id', $this->tank_auth->get_user_id());
    $query = $this->db->get('user_profiles');
    $data['row'] = $query->row();
}

In my controller I'm using $this->load->view('profile/edit_general_view', $data); to try and load the data from the model into the view.

function edit_profile()
{       

    //validation rules
    $this->form_validation->set_rules('first_name', 'First Name', 'trim|required|xss_clean|min_length[2]|max_length[20]|alpha');
    $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required|xss_clean|min_length[2]|max_length[20]|alpha');


    if ($this->form_validation->run() == FALSE) //if validation rule fails
    {           
        $this->load->view('profile/edit_general_view', $data); //load data from model in view
    }
    else //success
    {
        $send_to_db = array (                   
                'first_name'    => $this->input->post('first_name'),
                'last_name'     => $this->input->post('last_name')
        );
        $seg = 'edit';
        $this->load->model('Profile_model');
        $this->Profile_model->profile_update($send_to_db, $seg);            
    }
}

What is the correct way to pass the data from the model function profile_read into my controller function?

Upvotes: 0

Views: 131

Answers (1)

Aaria Carter-Weir
Aaria Carter-Weir

Reputation: 1679

Time to read about variable scopes: http://php.net/manual/en/language.variables.scope.php

The $data variable in the model method isn't accessible to the controller. Have the model method return an actual array of data.. eg:

function profile_read()
{
    $this->db->where('user_id', $this->tank_auth->get_user_id());
    $query = $this->db->get('user_profiles');
    return ($query ? $query->row : FALSE);
}

And then in the controller, store that in the 'data' for the view.

Upvotes: 3

Related Questions