phpcodeigniteruri

Codeigniter: Controller URI with Library


I have a working controller and library function, but I now need to pass a URI segment to the library for decision making, and I'm stuck.

Controller:

function survey($method)
{
    $id = $this->session->userdata('id');
    $data['member'] = $this->home_model->getUser($id);

    //Convert the db Object to a row array
    $data['manager'] = $data['member']->row();
    $manager_id = $data['manager']->manager_id;
    $data['manager'] = $this->home_model->getUser($manager_id);
    $data['manager'] = $data['manager']->row();
    if ($data['manager']->credits == '0')   {
        flashMsg('warning',"You can't complete the assessment until your manager has purchased credit.");
        redirect('home','location');
    }
    elseif ($data['manager']->test_complete == '3'){
        flashMsg('warning',"You already completed the Assessment.");
        redirect('home','location');                        
    }
    else{
        $data['header'] = "Home";
        $this->survey_form_processing->survey_form($this->_container,$data);
    }
}

Library:

function survey_form($container)
{
           if($method ==1){
    $id = $this->CI->session->userdata('id');
    // Setup fields
    for($i=1;$i<18;$i++){
      $fields["a_".$i] = 'Question '.$i;
    }
    for($i=1;$i<17;$i++){
      $fields["b_".$i] = 'Question '.$i;
    }
    $fields["company_name"] = "Company Name";
    $fields['company_address'] = "company_address";
    $fields['company_phone'] = "company_phone";
    $fields['company_state'] = "company_state";
    $fields['company_city'] = "company_city";
    $fields['company_zip'] = "company_zip";
    $fields['job_title'] = "job_title";
    $fields['job_type'] = "job_type";
    $fields['job_time'] = "job_time";
    $fields['department'] = "department";
    $fields['supervisor'] = "supervisor";
    $fields['vision'] = "vision";
    $fields['height'] = "height";
    $fields['weight'] = "weight";
    $fields['hand_dominance'] = "hand_dominance";
    $fields['areas_of_fatigue'] = "areas_of_fatigue";
    $fields['injury_review'] = "injury_review";
    $fields['job_positive'] = "job_positive";
    $fields['risk_factors'] = "risk_factors";
    $fields['job_improvement_short'] = "job_improvement_short";
    $fields['job_improvement_long'] = "job_improvement_long";
    $fields["c_1"] = "Near Lift";
    $fields["c_2"] = "Middle Lift";
    $fields["c_3"] = "Far Lift";
    $this->CI->validation->set_fields($fields);

    // Set Rules

    for($i=1;$i<18;$i++){
      $rules["a_".$i]= 'hour|integer|max_length[2]';
    }
    for($i=1;$i<17;$i++){
      $rules["b_".$i]= 'hour|integer|max_length[2]';
    }
    // Setup form default values
    $this->CI->validation->set_rules($rules);

        if ( $this->CI->validation->run() === FALSE )
        {
            // Output any errors
            $this->CI->validation->output_errors();
        }
        else
        {
            // Submit form
            $this->_submit();
        }
    // Modify form, first load
    $this->CI->db->from('be_user_profiles');
    $this->CI->db->where('user_id' , $id);
    $user = $this->CI->db->get();
    $this->CI->db->from('be_survey');
    $this->CI->db->where('user_id' , $id);
    $survey =   $this->CI->db->get();
    $user = array_merge($user->row_array(),$survey->row_array());
    $this->CI->validation->set_default_value($user);

    // Display page
    $data['user'] = $user;
    $data['header'] = 'Risk Assessment Survey';
    $data['page'] = $this->CI->config->item('backendpro_template_public') . 'form_survey';
    $this->CI->load->view($container,$data);
            }
            else{
                 redirect('home','location');
            }
}

My library function doesn't know what to do with Method...and I'm confused. Does it have something to do with instances in my library?


Solution

  • You can always call the URI Class in your library...

    $CI =& get_instance();
    
    // The segments from the url
    $uri_segments = $CI->uri->segments_array();
    
    // The segments from the route
    $ruri_segments = $CI->uri->rsegments_array();
    

    URI User Guide: http://codeigniter.com/user_guide/libraries/uri.html

    Also, in your library function survey_form, the variable $method isn't set. I guess you want to know how to set that to get what you want...? so do this:

    function survey_form($container)
    {
        $CI =& get_instance();
    
        $method = $CI->uri->segment(3): // or whichever segment you want
    
        if($method ==1)
        {
            ....