duanmen1887 2019-07-13 11:58 采纳率: 0%
浏览 79

如何设置下拉代码中选择的值codeigniter [复制]

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>
  • 写回答

1条回答 默认 最新

  • douhe5092 2019-07-13 12:35
    关注
    <select class="form-control" name="nama_stok">
    <option value="" selected>--Select--</option>
    <?php foreach ($getInventori as $a){?>
    <option value="<?= $a->id_inventori;?>" <?php echo set_select('nama_stok', $a->id_inventori, False); ?>> <?= $a->nama_inventori;?></option>
    <?php } ?>
    </select>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思