Skip to content

How to set select option value dynamically in PHP?

foreach($optionArr as $key => $value){
   $isSelected =""; //added this line
   if($_REQUEST['opt_side_'.$cside] == $value){
     $isSelected = "selected";
   }
   echo '<option value="'.$value.'"'.$isSelected.'>'.$key.'</option>';
}


//Laravel

@php
$optionArr = array ('Available' => 'Available', 'Not Available' => 'Not Available',  'Sold Out' => 'Sold Out',);
@endphp
<select id="{{ $eventPrice->id }}" class="drop form-control">
  @foreach ($optionArr as $key =>$value)
    @if ( $eventPrice->status == $value)
    	<option value="{{$value}}" class="" selected>{{$key}}</option>
    @else
    	<option value="{{$value}}" class="">{{$key}}</option>
    @endif
  @endforeach
</select>
See also  Python code snippet - How to create list ?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.