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?!
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 segmenthomeis the controller class name, the second segmentsurveyis the controller function name, and the third segmentnew(and any segments after this) is an ID or variable passed tosurvey(). 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$methodis always set to “update”. So,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$methodon 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):
$method="update"otherwise set$method="create"$methodto the other necessary functionsAn alternative solution might look like this:
survey(), set$methodbased on the data supplied$methodto the other necessary functionsI’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:
survey_form() function:
_submit() function:
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!