我有一个可用的控制器和库函数,但我现在需要将 URI 段传递给库以进行决策,但我陷入了困境。
控制器:
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);
}
}
图书馆:
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');
}
}
我的库函数不知道如何处理 Method...我很困惑。 这和我库中的实例有关系吗?
您始终可以调用库中的 URI 类...
$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 用户指南:http://codeigniter.com/user_guide/libraries/uri.html
此外,在您的库函数
survey_form
中,未设置变量$method
。我猜你想知道如何设置它以获得你想要的......?所以这样做:
function survey_form($container)
{
$CI =& get_instance();
$method = $CI->uri->segment(3): // or whichever segment you want
if($method ==1)
{
....