Fill a Select Multiple and Make Update in the Table

0

I have a <select multiple> that is filled with information from a table exams , the user selects the exams he wants for a certain function and sends to register in the table Okay.

My questions are:

  • How to display this retrieved data in an edit screen, ie leave the selected tests selected for the selected function.

  • How to make an update to this table function_exams ? Deleting the registered exams of that function and inserting the new ones?

Exams table: id, name, description

Function table: id, name, description

Examination_function_table: id, exam_id, id_functions

<select multiple id="exames" name="exames[]" class="form-control">
     @foreach($exames as $exame)
     <option value="{{ $exame->id }}">{{ $exame->nome }}</option>
     @endforeach
</select>
    
asked by anonymous 18.01.2016 / 18:30

1 answer

0

The fastest way to do this is:

  • Show all exams in checkboxes;

Loop the same way you are doing now (only with checkboxes), but check for each iteration of the loop if the ID is present in the function_exams table (a normal select), and if it is, put the checkbox as CHECKED , if not, no.

So your first problem is resolved: How to show the exams already selected and allow the user to give up or choose new exams.

  • Delete all records from the function_exams table and record the exams that the user selected.

When the user clicks the "Save" button (or something like that), delete all the records in the function_exams table that have the function id that the user is moving. After that, loop through all the exams that the user selected.

So your last problem is solved: How to update the table by deleting and inserting exams.

A hint for the edit screen: Put the checkboxes in a table with cool style and use a scrollbar div, if there are too many records.

    
20.01.2016 / 13:58