如何在编辑form_dropdown codeigniter中显示所选项目

时间:2015-02-18 05:05:10

标签: php mysql codeigniter select

我在更新/编辑菜单

时显示所选项目时出现问题

未选择该项目。怎么让它自动选择?

这是我的模特

    public function master_kategorilapor(){
    $this->db->order_by('id_kategorilapor');
    $sql_kategorilapor=$this->db->get('kategorilapor');
    if($sql_kategorilapor->num_rows()>0){
    return $sql_kategorilapor->result_array();
    }
}

这个控制器

    public function tambah_lapor(){
    $data['kategorilapor'] = $this->mymodel->master_kategorilapor();
    $dd_kategorilapor = array();
    foreach ($this->mymodel->master_kategorilapor() as $data_kategorilapor) 
    {
    $dd_kategorilapor[$data_kategorilapor['id_kategorilapor']] = $data_kategorilapor['nama_kategorilapor'];
    }
    $data['kategorilapor']=$dd_kategorilapor;

    $this->load->view ('petugas/tambah_lapor', $data);
}

控制器获取更新数据

public function update_data ($id_lapor){


$this->load->model("mymodel");
$mhs = $this -> mymodel -> GetLapor("where id_lapor = '$id_lapor' ");
$data = array(
    "id_lapor" => $mhs[0]['id_lapor'],
    "tgl_lapor" => $mhs[0]['tgl_lapor'],
    "t1" => $mhs[0]['t1'],
    "t2" => $mhs[0]['t2'],
    "dari" => $mhs[0]['dari'],
    "untuk" => $mhs[0]['untuk'],
    "id_tujuanlapor" => $mhs[0]['id_tujuanlapor'],
    "id_kategorilapor" => $mhs[0]['id_kategorilapor'],
    "isi_taruna" => $mhs[0]['isi_taruna'],
    "keterangan_taruna" => $mhs[0]['keterangan_taruna']);


$this->load->view('petugas/update_lapor',$data);

}

这是我的观点

     <?php echo form_dropdown("id_kategorilapor",$kategorilapor); ?>

1 个答案:

答案 0 :(得分:2)

让我们看一下下拉方法的基本形式。

$dd_list = array(
                  'Mr'   => 'Mr',
                  'Mrs'   => 'Mrs',
                  'Miss'   => 'Miss',
                 );
 echo form_dropdown('title', $dd_list, 'Mr');  

这里我们用标题列表创建了数组。然后我们在表单中打印下拉列表。

假设我们想在我们的数据库中为Mr,2为Mrs和3为Miss存储键值1,我们将以上代码修改为:

 $dd_list = array(
                  '1'   => 'Mr',
                  '2'   => 'Mrs',
                  '3'   => 'Miss',
                );
 echo form_dropdown('title', $dd_list, '3');

上面我们设置了3:Miss作为默认选择值。

让我们将set_value函数的基本形式添加到上面的代码中。因此,表单将记住在提交不完整的情况下提交的内容。

$dd_list = array(
                  '1'   => 'Mr',
                  '2'   => 'Mrs',
                  '3'   => 'Miss',
                );

 $dd_name = "title";

 echo form_dropdown($dd_name, $dd_list, '3');

<强>最后

 $dd_list = array(
                  '1'   => 'Mr',
                  '2'   => 'Mrs',
                  '3'   => 'Miss',
                );

 $dd_name = "title";
 $sl_val = $this->input->post($dd_name);

echo form_dropdown($dd_name, $dd_list, set_value($dd_name, ( ( !empty($sl_val) ) ? "$sl_val" : 3 ) ) );