1

I have a select input that allows for multiple options to be selected. Because of the simple requirements, these selections are just stored as an array in my MySQL field. So for example they are stored as:

[Retail, Wholesale]

Now my question is, if I wanted the user to be able to edit the specific record, how would I pull the options up as selected in the select input?

The select field is as such:

<select class="form-control" name="type_industry">
<option value="Retail">Retail</option>
<option value="Wholesale">Wholesale</option>
<option value="Service">Service</option>
</select>

This is in a Laravel blade template, but if necessary I'm willing work with pure php answers or even javascript, I just need to be able to move past this issue.

Thanks!

0

2 Answers 2

2

You can use in_array() function,

<select class="form-control" name="type_industry">
<option value="Retail" @if(in_array("Retail", $selected_items_array)) selected @endif>Retail</option>
<option value="Wholesale" @if(in_array("Wholesale", $selected_items_array)) selected @endif>Wholesale</option>
<option value="Service" @if(in_array("Service", $selected_items_array)) selected @endif>Service</option>
</select>
Sign up to request clarification or add additional context in comments.

Comments

0

if [Retail, Wholesale] is a php array ($savedOptions), and you have a list of all available options ($availableOptions) as another array, pass them to the blade view and in your blade just loop through:

@foreach ( $savedOptions as $value )
    <option {{ in_array($value,$availableOptions) ? 'selected' : '' }} value={{ $value }}>{{ $value }}</option>
@endforeach 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.