I have a function like below, there is a block of code for each branch in conditional statement, I'd like to be able to just call that block of code once and then refer to it via an array, but I am a little confused how to do this. The function resides in a class.
function do_upload()
{
$config['upload_path'] = 'assets/temp';
$config['allowed_types'] = 'csv';
$config['max_size'] = '0';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$company_id = $this->company_info->get_co_id($this->dx_auth->get_user_id());
$company_name = $this->company_info->get_company_name($company_id);
$data['title'] = "Import Users";
$data['header']= "Import Users";
$data['header_logo'] = $this->company_info->which_company_logo($this->dx_auth->get_user_id());
$main_data['custom_text'] = $this->_custom_text;
$main_data['general_text'] = $this->_general_text;
$main_data['custom_color'] = $this->company_info->get_cached_co_color($company_name);
$main_data['company_id'] = $company_id;
//Display errors if any
$main_data['error'] = $this->upload->display_errors();
$data['main'] = $this->load->view('adm/import_new_user_error', $main_data, TRUE);
$this->load->view('template', $data);
}
else
{
$company_id = $this->company_info->get_co_id($this->dx_auth->get_user_id());
$company_name = $this->company_info->get_company_name($company_id);
$data['title'] = "Import Users";
$data['header']= "Import Users";
$data['header_logo'] = $this->company_info->which_company_logo($this->dx_auth->get_user_id());
$main_data['custom_text'] = $this->_custom_text;
$main_data['general_text'] = $this->_general_text;
$main_data['custom_color'] = $this->company_info->get_cached_co_color($company_name);
$main_data['company_id'] = $company_id;
//Display data on uploaded file
$main_data['upload_data'] = $this->upload->data();
$data['main'] = $this->load->view('adm/import_user_sucess', $main_data, TRUE);
$this->load->view('template', $data);
}
}
This is the block of code I am talking about. I tried to put it into static function but was not working.
$company_id = $this->company_info->get_co_id($this->dx_auth->get_user_id());
$company_name = $this->company_info->get_company_name($company_id);
$data['title'] = "Import Users";
$data['header']= "Import Users";
$data['header_logo'] = $this->company_info->which_company_logo($this->dx_auth->get_user_id());
$main_data['custom_text'] = $this->_custom_text;
$main_data['general_text'] = $this->_general_text;
$main_data['custom_color'] = $this->company_info->get_cached_co_color($company_name);
$main_data['company_id'] = $company_id;
To make use of common code only once in your program, you have to change your code as below.