Fill a Select Multiple and Update Table

Asked

Viewed 366 times

0

I have a <select multiple> which 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 exams, okay.

My doubts are:

  • How to show this recovered data on an editing screen, ie leave as selected the exams that are registered for the selected function.

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

Table exams: id, name, description

Table function: id, name, description

Table function_exams: id, id_exame, id_funcao

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

1 answer

0

The fastest way to do that is:

  • Show all exams in checkboxes;

Loop in the same way you’re looping now (only with checkboxes), but check each loop iteration to see if the ID is present in the table funcao_exames (a normal select) and, if it is, set the checkbox as CHECKED, if not, no.

Thus solved is its first problem: How to show the exams already selected and allow the user to give up or choose new exams.

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

When the user clicks on the "Save" button (or something like that), delete all records from the parse_scan table that have the id of the function the user is moving. After that, loop and register all the exams that the user selected.

Thus solved your last problem: How to update table by deleting and inserting scans.

A tip for the edit screen: Place the checkboxes in a cool style table and use a scrollbar div if there are too many records.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.