Igniter code form_multiselect

I have a completed Code Igniter form_multiselect. With values โ€‹โ€‹in it

The code for this is as follows:

<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> 

This is done on a system where you can send messages to multiple users. Therefore, I would like to say that the user selects a value of 1 and a value of 8. I would like to start two inserts with the same data, but the only difference is that the EMPLOYEES_id field in the database is different every time.

My model is as follows:

 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'); } 

Thanks for the help in advance.

+4
source share
2 answers

The easiest way is to set all the general parameters, and then insert the data for each foreach iteration $this->input->post('EMPLOYEES_id') .

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

$employees is actually an array of identifiers, so you have to repeat it (use foreach). Do var_dump($employees) yourself to check its structure.

The final code should look like this:

 // 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
source

Source: https://habr.com/ru/post/1413955/


All Articles