I'm creating a profile script where users can edit their personal info, interests, and links.
I had all the fields in one form but now I want to separate them by tabs. So I will have a personal info tab, interests tab, and links tab. In each page I will have a form submitting data to the corresponding function. For example if you're editing the personal info the form will direct to mysite.com/edit/personal_info
The functions should look like this
function edit() {
function personal_info() {
//data
}
function interests() {
//data
}
function links() {
//data
}
}
I'm not sure how to properly send data from the edit() function to all its sub functions.
I'm adding the general data below to all my function but I want to add it once and all the functions should have it. I'm also trying to avoid global variables.
$this->db->where('user_id', $this->tank_auth->get_user_id());
$query = $this->db->get('user_profiles');
$data['row'] = $query->row();
Then in each sub function I have validation rules (codeigniter) Below are the rules for the personal_info function
$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');
$this->form_validation->set_rules('gender', 'Gender', 'trim|required|xss_clean|alpha');
and a statement to add the data to the database or return an error if validation rules fail
if ($this->form_validation->run() == FALSE) //if validation rules fail
{
$this->load->view('edit_profile', $data);
}
else //success
{
$data = array (
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'gender' => $this->input->post('gender')
);
$this->load->model('Profile_model');
$this->Profile_model->profile_update($data);
}
How can I properly create these sub function without repeating code in each one?