简体   繁体   中英

how to show selected item in edit form_dropdown codeigniter

i have a problem to display the the selected item when at update/edit menu

the item was not selected. how make it auto select?

here my model

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

this the controller

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

controller get update 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);

}

And this is my view

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

Lets look at the basic form of dropdown method.

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

Here we created the array with the list of titles. We then printed the dropdown list in the form.

Suppose, we want to store the key value 1 for Mr, 2 for Mrs and 3 for Miss in our database, we would modify the above code as:

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

Above we have set 3:Miss as default selected value.

Lets add basic form of set_value function to above code. So the form will remember what was submitted in case of incomplete submission.

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

 $dd_name = "title";

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

Finally

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM