1
我有一個像下面這樣的函數,在條件語句中每個分支都有一段代碼,我希望能夠只調用一段代碼,然後通過數組,但我有點困惑如何做到這一點。該函數駐留在一個類中。在CI中使用靜態函數
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);
}
}
這是我說的代碼塊。我試圖把它變成靜態函數,但沒有工作。
$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;
'if'和'else'部分只有一行代碼差異。這是'$ main_data ['error']'和'$ main_data ['upload_data']'。對?你想讓'if'和'else'的通用代碼? –
不,我想調用所有if和else之間相同的事情,而不是一次if和else一次。 –