This question already has an answer here:
I have update page with text input and dropdown. It work with getting data from my database to the field only with the textbox. the dropdown not show any selected data, so i don't know which value that i use now.
this is my view: it give all select option "selected value".
<select class="form-control" name="nama_stok">
<option value="">Pilih</option>
<?php if(count($getInventori)):?>
<?php foreach ($getInventori as $a):?>
<option value="<?= $a->id_inventori;?>" selected="selected" > <?= $a->nama_inventori;?> </option>
<?php endforeach;?>
<?php else :?>
<?php endif ;?>
</select>
i also have tried the code bellow but its show some error so i have no idea.
code that i've tried:
<select class="form-control" name="nama_stok">
<option value="">Pilih</option>
<?php if(count($getInventori)):?>
<?php foreach ($getInventori as $a):?>
<option value="<?= $a->id_inventori;?>"
<?php if($a->id_inventori == $data[0]->id_inventori) echo 'selected="selected"'; ?>>
<?= $a->nama_inventori;?></option>
<?php endforeach;?>
<?php else :?>
<?php endif ;?>
</select>
so what i want is show selected value in my dropdown.
e.g. i have value 1,2,3,4,5 in my dropdown. Then the selected value is 5. when i open update page, the default dropdown value must be 5.
anyone can help ?
</div>