I want to display the already selected option when editing my data, but it doesn't show the correct chosen option. how do i show the correct option?
My code:
<div >
<label for="Jurusan" >Jurusan</label>
<div >
<select id="jurusan" name="jurusan">
<option value="" >Pilih</option>
<?php foreach($jurusan as $row):?>
<option <?php if($row->id_jurusan == $row->id_jurusan){ echo 'selected="selected"'; } ?> value="<?php echo $row->id_jurusan; ?>"><?php echo $row->nama_jurusan;?> </option>
<?php endforeach;?>
</select>
<small >
<?php echo form_error('jurusan') ?>
</small>
</div>
</div>
CodePudding user response:
It seems like i found the answer, the correct code should be this
<option <?php if($row->id_jurusan == $data_form->id_jurusan){ echo 'selected="selected"'; } ?> value="<?php echo $row->id_jurusan; ?>"><?php echo $row->nama_jurusan;?> </option>
$data_form is from my controller, to get the primary key
$data["data_form"] = $form->getById($id);
CodePudding user response:
Just you this
<option <?php if($row->id_jurusan == $data_form->id_jurusan){ echo 'selected'; } ?> value="<?php echo $row->id_jurusan; ?>"><?php echo $row->nama_jurusan;?> </option>
You can use this code.
