1

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;
tereško
  • 58,060
  • 25
  • 98
  • 150
the tao
  • 253
  • 2
  • 6
  • 13
  • there is only one line of code difference in `if` and `else` section. That is `$main_data['error']` and `$main_data['upload_data']`. right? You want make the common code for both `if` and `else` ? – Kumar V Dec 16 '13 at 09:38
  • No, I want to call all the things that are the same between if and else once, not once for if and once for else. – the tao Dec 16 '13 at 18:49

1 Answers1

1

To make use of common code only once in your program, you have to change your code as below.

function do_upload()
{
    $config['upload_path'] = 'assets/temp';
    $config['allowed_types'] = 'csv';
    $config['max_size'] = '0';

    $this->load->library('upload', $config);
    $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 ( ! $this->upload->do_upload())
    {


        //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
    {

        //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);
    }
}
Kumar V
  • 8,810
  • 9
  • 39
  • 58