php - Display selected option in codeigniter

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 class="form-group row">
                        <label for="Jurusan" class="col-sm-2 col-form-label">Jurusan</label>
                        <div class="col-sm-10">
                            <select class="form-control" 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 class="text-danger">
                                <?php echo form_error('jurusan') ?>
                            </small>
                        </div>
                    </div>

Answer

Solution:

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);

Answer

Solution:

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.

Source