Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 789601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:33:01+00:00 2026-05-14T21:33:01+00:00

I’m using URIs to direct a function in a library… View File (It’s huge,

  • 0

I’m using URIs to direct a function in a library…
View File (It’s huge, so I’m just posting the opening line):

<?php print form_open('survey',array('class'=>'horizontal','id'=>'form_survey'))?>

Library:

function survey_form($container)
{
    $id = $this->CI->session->userdata('id');
    // Setup fields
    for($i=1;$i<18;$i++){
      $fields["a_".$i] = 'Question '.$i;
    }
    for($i=1;$i<24;$i++){
      $fields["b_".$i] = 'Question '.$i;
    }
    $fields["firstname"] = "First Name";
    $fields["lastname"] = "Last Name";
    $fields["phone"] = "Phone";
    $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['job_description'] = "job_description";
    $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<24;$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();
        }
    //Tool for current user
    if ($method == 'update') {
        // 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);
}

Submit function:

function _submit()
{
    $URI = $this->CI->uri->uri_string();
    $new = "new";
    if(strpos($URI, $new) === FALSE){
        $method = "update";
    }
    elseif(strpos($URI, $new) !== FALSE){
        $method = "create";
    }

    //Submit and Update for current User
    $id = $this->CI->session->userdata('id');
    $this->CI->db->select('users.id, users.username, users.email, profiles.firstname, profiles.manager_id');
    $this->CI->db->from('be_users' . " users");
    $this->CI->db->join('be_user_profiles' . " profiles",'users.id=profiles.user_id');
    $this->CI->db->having('id', $id);
    $email_data['user'] = $this->CI->db->get();
    $email_data['user'] = $email_data['user']->row();
    $manager_id = $email_data['user']->manager_id;
    $this->CI->db->select('firstname','email')->from('be_user_profiles')->where('user_id', $manager_id);

    $email_data['manager'] = $this->CI->db->get();
    $email_data['manager'] = $email_data['manager']->row();


    // Fetch what they entered in the form

    for($i=1;$i<18;$i++){
      $survey["a_".$i]= $this->CI->input->post('a_'.$i);
    }
    for($i=1;$i<24;$i++){
      $survey["b_".$i]= $this->CI->input->post('b_'.$i);
    }
    for($i=1;$i<12;$i++){
      $survey["c_".$i]= $this->CI->input->post('c_'.$i);
    }
    $profile['firstname'] = $this->CI->input->post('firstname');
    $profile['lastname'] = $this->CI->input->post('lastname');
    $profile['test_date'] = date ("Y-m-d H:i:s");
    $profile['company_name'] = $this->CI->input->post('company_name');
    $profile['company_address'] = $this->CI->input->post('company_address');
    $profile['company_city'] = $this->CI->input->post('company_city');
    $profile['company_phone'] = $this->CI->input->post('company_phone');
    $profile['company_state'] = $this->CI->input->post('company_state');
    $profile['company_zip'] = $this->CI->input->post('company_zip');
    $profile['job_title'] = $this->CI->input->post('job_title');
    $profile['job_type'] = $this->CI->input->post('job_type');
    $profile['job_time'] = $this->CI->input->post('job_time');
    $profile['department'] = $this->CI->input->post('department');
    $profile['vision'] = $this->CI->input->post('vision');
    $profile['height'] = $this->CI->input->post('height');
    $profile['weight'] = $this->CI->input->post('weight');
    $profile['hand_dominance'] = $this->CI->input->post('hand_dominance');
    $profile['areas_of_fatigue'] = $this->CI->input->post('areas_of_fatigue');
    $profile['job_description'] = $this->CI->input->post('job_description');
    $profile['injury_review'] = $this->CI->input->post('injury_review');
    $profile['job_positive'] = $this->CI->input->post('job_positive');
    $profile['risk_factors'] = $this->CI->input->post('risk_factors');
    $profile['job_improvement_short'] = $this->CI->input->post('job_improvement_short');
    $profile['job_improvement_long'] = $this->CI->input->post('job_improvement_long');      

    if ($method == "update") {
        //Begin db transmission
        $this->CI->db->trans_begin();

        $this->CI->home_model->update('Survey',$survey, array('user_id' => $id));
        $this->CI->db->update('be_user_profiles',$profile, array('user_id' => $id));        
        if ($this->CI->db->trans_status() === FALSE)
        {
            flashMsg('error','There was a problem entering your test! Please contact an administrator.');
            redirect('survey','location');
        }
        else
        {
            //Get credits of user and subtract 1        
            $this->CI->db->set('credits', 'credits -1', FALSE);
            $this->CI->db->update('be_user_profiles',$profile, array('user_id' => $manager_id));

            //Mark the form completed.
            $this->CI->db->set('test_complete', '1');
            $this->CI->db->where('user_id', $id)->update('be_user_profiles');
            // Stuff worked...
            $this->CI->db->trans_commit();



            //Get Manager Information
            $this->CI->db->select('users.id, users.username, users.email, profiles.firstname');
            $this->CI->db->from('be_users' . " users");
            $this->CI->db->join('be_user_profiles' . " profiles",'users.id=profiles.user_id');
            $this->CI->db->having('id', $email_data['user']->manager_id);
            $email_data['manager'] = $this->CI->db->get();
            $email_data['manager'] = $email_data['manager']->row();

            //Email User
            $this->CI->load->library('User_email');
            $data_user = array(
                    'firstname'=>$email_data['user']->firstname,
                    'email'=> $email_data['user']->email,
                    'user_completed'=>$email_data['user']->firstname,
                    'site_name'=>$this->CI->preference->item('site_name'),
                    'site_url'=>base_url()
            );

            //Email Manager     
            $data_manager = array(
                    'firstname'=>$email_data['manager']->firstname,
                    'email'=> $email_data['manager']->email,
                    'user_completed'=>$email_data['user']->firstname,
                    'site_name'=>$this->CI->preference->item('site_name'),
                    'site_url'=>base_url()
            );
            $this->CI->user_email->send($email_data['manager']->email,'Completed the Assessment Tool','public/email_manager_complete',$data_manager);
            $this->CI->user_email->send($email_data['user']->email,'Completed the Assessment Tool','public/email_user_complete',$data_user);

            flashMsg('success','You finished the assessment successfully!');
            redirect('home','location');
        }
    }
    //Create New User
    elseif ($method == "create") {
            // Build
            $profile['user_id'] = $id;
            $profile['manager_id'] = $manager_id;
            $profile['test_complete'] = '1';
            $survey['user_id'] = $id;
            $this->CI->db->trans_begin();

            // Add user_profile details to DB
            $this->CI->db->insert('be_user_profiles',$profile);
            $this->CI->db->insert('be_survey',$survey);

            if ($this->CI->db->trans_status() === FALSE)
            {
                // Registration failed
                $this->CI->db->trans_rollback();

                flashMsg('error',$this->CI->lang->line('userlib_registration_failed'));
                redirect('auth/register','location');
            }
            else
            {
                // User registered
                $this->CI->db->trans_commit();

                flashMsg('success',$this->CI->lang->line('userlib_registration_success'));
                redirect($this->CI->config->item('userlib_action_register'),'location');
            }

    }
}

Controller function:

function survey()
{
    $id = $this->session->userdata('id');
    $data['member'] = $this->home_model->getUsers(array('user_id'=>$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->getUsers(array('manager_id'=>$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);
    }
}

The submit function is similar, updating the db if $method == “update”, and inserting if the method == “create”.

The problem is, when the form is submitted, it doesn’t take into account the url b/c the form submits to the function “survey”, which passes data to the lib function, so things are always updated, never created.

How can I pass $method to the _submit() function correctly?!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:33:02+00:00Added an answer on May 14, 2026 at 9:33 pm

    I have a better understanding of your problem now – thanks for posting the updates to your code. I’m updating and elaborating on my answer to explain your problem and offer some solutions.

    I’m sure you know, but it won’t hurt to preface this with a basic review of the general MVC pattern as it relates to URI structure. That is, in the example of site.com/home/survey/new, the first segment home is the controller class name, the second segment survey is the controller function name, and the third segment new (and any segments after this) is an ID or variable passed to survey(). More here.

    Your code isn’t working because you never pass a variable to survey() via the URI when you submit the form, so “new” never exists in the URL, and therefore $method is always set to “update”. So,

    “How can I pass $method to the
    _submit() function correctly?”

    There are a couple of options for doing this. Each one simply requires that you determine whether a user->survey relationship exists in order to set $method. You can then pass $method on to the appropriate functions. For my example below, I’m going to assume that a user can only have one survey. If that’s not the case you’ll just have to account for a unique survey ID associated with a unique user ID.

    One possible scenario goes like this (again, this assumes that a user can be associated with only one survey):

    1. User submits survey
    2. Upon form submission, check to see if a survey already exists for that user
    3. If so, set $method="update" otherwise set $method="create"
    4. Proceed to pass $method to the other necessary functions

    An alternative solution might look like this:

    1. In your controller function that calls the view file of your survey, determine if a record already exists for the user.
    2. Pass that data to your survey view
    3. In the view, modify the form action so that it passes that data (either “create” or “update”) to the survey() function when the form is submitted.
    4. In survey(), set $method based on the data supplied
    5. Proceed to pass $method to the other necessary functions

    I’ll use the first option as an example and outline the logic you would need (forgive the somewhat pseudo-code, but you’ll get the idea):

    survey() function:

    function survey() {
    
        // instead of determining whether "new" is part of the URI (which it never is),
        // check for an existing survey associated with the logged in user's ID
        $id = $this->session->userdata('id');
        $query = $this->db->get_where('Survey', array('user_id' => $id));
    
        // if the above query returns a result, set $method="update"
        // otherwise set $method="create"
        if ($query->num_rows() > 0) {
            $method="update";
        } else {
            $method="create";
        }
    
        // whatever other logic you need here
    
        // pass $method to survey_form()
        $this->survey_form_processing->survey_form($this->_container, $data, $method);
    }
    

    survey_form() function:

    function survey_form($container, $data, $method) {
    
        // your other code here
    
        if ( $this->CI->validation->run() === FALSE ) {
            // Output any errors
            $this->CI->validation->output_errors();
        } else {
            // Submit form (w/ $method parameter - this comes from survey())
            $this->_submit($method);
        }
    
    }
    

    _submit() function:

    function _submit($method) {
    
        // your other code here
    
        if ($method == "update") {
            // do something
        } elseif ($method == "create") {
            // do something else
        }
    
    }
    

    As a side note, there are probably better ways to structure what you’re doing. For example, in my pseudo-code above, the database interactions should probably be happening in a model. For clarity, though, they are included in the controller. I hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 391k
  • Answers 391k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer BOOL CALLBACK EnumChildProc ( HWND hwndChild, LPARAM lParam) { //… May 15, 2026 at 1:17 am
  • Editorial Team
    Editorial Team added an answer Why not have a dev server that each dev connects… May 15, 2026 at 1:17 am
  • Editorial Team
    Editorial Team added an answer You have to assign an eventhandler on the datagrid, and… May 15, 2026 at 1:17 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.