有人帮助我,当编辑归档选择时,我无法从数据库获得价值

时间:2016-06-27 06:06:18

标签: php html codeigniter

我创建了一个选择选项表单但是在编辑时我可以从数据库中获取当前值。

 <select name="t_proyek_kd_proyek" id="t_proyek_kd_proyek" class="select-search"  />
        <option value=""></option>
        <?php 
        $t_cost=$this->db->query("select * from t_proyek ");

        foreach($t_cost->result() as $value){
          $selected= '';
          if($nm_proyek == $value->nm_proyek){
            $selected = 'selected';

          }
        ?>
          <option value="<?php echo $value->kd_proyek; ?>" <?php echo $selected;?>>
            <?php echo $value->nm_proyek; ?>
          </option>
       <?php }?>

      </select>

2 个答案:

答案 0 :(得分:0)

您不应该为select标记使用值。检查选项标签内的条件,并选择如下

<select name="t_proyek_kd_proyek" id="t_proyek_kd_proyek" class="select-search"  placeholder="Pilih..." />
    <option value=""></option>
    <?php 
    $t_cost=$this->db->query("select * from t_proyek ");
    foreach($t_cost->result() as $value){?>
        <option value="<?php echo $value->kd_proyek; ?>" <?php if($nm_proyek==$value->kd_proyek) { ?> selected="selected" <?php }?> ><?php echo $kd_proyek=$value->nm_proyek; ?></option>
    <?php }?>
</select>

答案 1 :(得分:0)

<?php 
$conn = new mysqli('SERVER NAME', 'DB USER NAEM', 'DB USER PASSWORD', 'DB NAME');
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
//set the value of which option you want to selected
$nm_proyek = '';
?>
<!-- for combobox  you should use 'multiple' -->
<select name="t_proyek_kd_proyek" id="t_proyek_kd_proyek" class="select-search" placeholder="Pilih..." multiple />
    <option value=""></option>
    <?php
    $query = $conn->query("SELECT * FROM `t_proyek`");
    while($value = $query->fetch_object()) {?>
        <option value="<?php echo $value->kd_proyek; ?>" <?php $nm_proyek == $value->kd_proyek ? "selected='selected'" : "" ?> ><?php echo $kd_proyek=$value->nm_proyek; ?></option>
    <?php }?>
</select>