2012-05-23 3 views
4

채워진 코드 이그니 터 form_multiselect가 있습니다. 이것은 여러 사용자에게 메시지를 보낼 수있는 시스템에서 실행코드 이그니 터 form_multiselect

<select multiple="multiple" name="EMPLOYEES_id[]"> 
<option value="8">Employee 8</option> 
<option value="1">Employee 1</option> 
<option value="12">Employee 12</option> 
</select> 

을 다음과 같이 그것의 값으로 그에 대한

코드입니다. 그래서 내가하고 싶은 것은 사용자가 값 1과 값 8을 선택한다고 가정합니다. 동일한 데이터를 가진 두 개의 삽입을 실행하고 싶지만 데이터베이스의 EMPLOYEES_id 필드는 매번 다를 수 있습니다. 사전에 도움을

function create() 
{ 


    $this->template->set('title', 'Create Message'); 


    // Validate the form input 
    $this->form_validation->set_error_delimiters('<span class="help-inline">', '</span>'); 
    $this->form_validation->set_rules('MESSAGE_summary', 'Message Summary', 'trim|required|xss_clean'); 
    $this->form_validation->set_rules('MESSAGE_body', 'Message Body', 'trim|required|xss_clean'); 


    if($this->form_validation->run() == FALSE) 
    { 

     // Get all users in same company as logged in user 
     $comapny_contacts = $this->Employees_model->read_company_contacts($this->viewinguser->BUSINESSES_id); 
     $comapny_contacts = $comapny_contacts->result(); 

     foreach($comapny_contacts as $contacts) 
     { 
      $comapny_contacts_options[$contacts->EMPLOYEES_id] = $contacts->EMPLOYEES_firstname . ' ' . $contacts->EMPLOYEES_surname; 
     } 

     $this->template->set('comapny_contacts_options', $comapny_contacts_options); 



     // form has not been run or there are validation errors 
     $this->template->build('messages/create'); 
    } 
    else 
    { 

     /* 
     * Lets create the new message in the database 
     */ 
     $insert_data = new stdClass(); 
     $insert_data->MESSAGES_summary = $this->input->post('MESSAGE_summary'); 
     $insert_data->MESSAGES_body = $this->input->post('MESSAGE_body'); 
     $insert_data->MESSAGES_level = $this->input->post('MESSAGES_level'); 
     $insert_data->MESSAGES_type = $this->input->post('MESSAGES_type'); 
     $insert_data->MESSAGES_createdby = $this->activeuser->EMPLOYEES_id; 
     //$insert_data->EMPLOYEES_id = $this->input->post('EMPLOYEES_id'); 
     foreach($this->input->post('EMPLOYEES_id') as $employee_id) 
     { 
      $insert_data->EMPLOYEES_id = $employee_id; 

      // Save to new message in the database 
      $this->Messages_model->create($insert_data); 
     } 

     /* 
     * Now lets save a notification email in the database to be sent to the user 
     */ 
     $notification_body = ' 
     <p>A new message has been added for you by ' . $this->session->userdata('activeuser')->EMPLOYEES_firstname . '<br /> 
     ------------------------------</p> 
     <p><strong>Summary:</strong> ' . $this->input->post('MESSAGE_summary') . '</p> 
     <p>------------------------------<br /> 
     <a class="btn" href="' . base_url('messages') . '">Click to view full messages</a>'; 

     // Get the info for the user who the notification is for 
     $notify_user_info = $this->Employees_model->read_single_switch($this->input->post('EMPLOYEES_id')); 
     $notify_user_info = $notify_user_info->result(); 

     // Set all the data 
     $insert_data = new stdClass(); 
     $insert_data->NOTIFICATIONS_toname = $notify_user_info[0]->EMPLOYEES_firstname . ' ' . $notify_user_info[0]->EMPLOYEES_surname; 
     $insert_data->NOTIFICATIONS_toemail = $notify_user_info[0]->EMPLOYEES_email; 
     $insert_data->NOTIFICATIONS_subject = 'A new message has been added for you'; 
     $insert_data->NOTIFICATIONS_htmlbody = $notification_body; 
     //$insert_data->EMPLOYEES_id = $this->input->post('EMPLOYEES_id'); 
     foreach($this->input->post('EMPLOYEES_id') as $employee_id) 
     { 
      $insert_data->EMPLOYEES_id = $employee_id; 

      // Save to new notification to the database 
      $this->Notifications_model->create($insert_data); 
     } 

     // show user confirmation 
     $this->session->set_flashdata('success_message', 'New message created successfully.'); 
     redirect('messages'); 



    } 

감사를 다음과 같이

내 모델이다.

답변

2

가장 쉬운 방법은 일반적인 모든 매개 변수를 설정 한 후 $this->input->post('EMPLOYEES_id')의 각 foreach 반복에 데이터를 삽입하는 것입니다.

foreach($this->input->post('EMPLOYEES_id') as $eid){ 
     // insert data using $uid as the employer id here 
} 
0
$employees = $this->input->post('EMPLOYEES_id') 

$employees 실제로 ID의 배열, 그래서 당신은 그것을 (사용 foreach 문)을 반복해야합니다. var_dump($employees)을 직접 확인하여 구조를 확인하십시오.

마지막 코드는 다음과 같아야합니다

// Set all the data 
$insert_data = new stdClass(); 
$insert_data->NOTIFICATIONS_toname = $notify_user_info[0]->EMPLOYEES_firstname . ' ' . $notify_user_info[0]->EMPLOYEES_surname; 
$insert_data->NOTIFICATIONS_toemail = $notify_user_info[0]->EMPLOYEES_email; 
$insert_data->NOTIFICATIONS_subject = 'A new message has been added for you'; 
$insert_data->NOTIFICATIONS_htmlbody = $notification_body; 
foreach($this->input->post('EMPLOYEES_id') as $employee_id) 
{ 
    $insert_data->EMPLOYEES_id = $employee_id; 

    // Save to new notification to the database 
    $this->Notifications_model->create($insert_data); 
} 
+0

안녕하세요, 전체 컨트롤러로 질문을 업데이트합니다. 하지만 지금은 DB 오류가 발생했습니다. 데이터베이스를 보면 정보가 들어 있지만 거기에 "Array()"값이있는 where 절이 있습니다. "BUSINESSES_ID' WHERE 'EMPLOYEES_ID' = Array" – StuBlackett

+0

당신은 당신의 사업체를 선택합니다. "사업체.", "사업체". 이전과 같은 실수를 저지르고있다. 단일 ID 값 대신 ID 배열을 전달하고 있습니다. 활성 레코드를 사용하는 경우 $ this-> db-> 대신 $ this-> db-> where_in을 사용하십시오. – Skyrpex

관련 문제