我正在使用URI来指导库中的函数... 查看文件(这是巨大的,所以我只是发布开头行):
<?php print form_open('survey',array('class'=>'horizontal','id'=>'form_survey'))?>
库:
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);
}
提交功能:
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');
}
}
}
控制器功能:
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);
}
}
提交函数类似,如果$ method ==“update”则更新db,如果方法==“create”则更新。
问题,提交表单时,它没有考虑表单提交到函数“survey”的url b / c,它将数据传递给lib函数,所以事情总是更新,永远不会创建。
如何正确地将$方法传递给_submit()函数?!
答案 0 :(得分:3)
我现在对您的问题有了更好的了解 - 感谢您将更新发布到您的代码中。我正在更新并详细说明我的答案,以解释您的问题并提供一些解决方案。
我相信你知道,但是通过对与URI结构相关的一般MVC模式的基本回顾来介绍这一点并不会有什么坏处。也就是说,在site.com/home/survey/new
的示例中,第一个段home
是控制器类名,第二个段survey
是控制器函数名,第三个段new
(以及之后的任何段)是传递给survey()
的ID或变量。 More here
您的代码无效,因为您在提交表单时从未通过URI将变量传递给survey()
,因此URL中永远不会存在“new”,因此始终会设置$method
更新”。所以,
“我如何将$方法传递给 _submit()函数是否正确?“
这样做有几种选择。每个人只需要确定是否存在用户 - >调查关系以便设置$method
。然后,您可以将$method
传递给相应的函数。对于下面的示例,我将假设用户只能进行一次调查。如果不是这种情况,您只需要考虑与唯一用户ID关联的唯一调查ID。
一种可能的情况是这样的(再次,这假设用户只能与一个调查相关联):
$method="update"
,否则设置$method="create"
$method
传递给其他必要的功能替代解决方案可能如下所示:
survey()
中,根据提供的数据设置$method
$method
传递给其他必要的功能我将使用第一个选项作为示例并概述您需要的逻辑(原谅有些伪代码,但您会明白这一点):
survey()函数:
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 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 _submit($method) {
// your other code here
if ($method == "update") {
// do something
} elseif ($method == "create") {
// do something else
}
}
作为旁注,可能有更好的方法来构建您正在做的事情。例如,在我上面的伪代码中,数据库交互可能应该在模型中发生。但是为了清楚起见,它们包含在控制器中。我希望有所帮助!