简体   繁体   中英

codeigniter form_dropdown, set option value without an array

I want to fetch a table which has 2 columns: id and name, and I want column id as value for each option, and column name as option name

This is my controller

  //populate provinsi $this->load->model('Provinsi'); $provinsi = $this->Provinsi->get(); $this->load->view('admin/pages/product_form', array( 'provinces' => $provinsi, )); 

And in my view

 <?php $options_provinsi = array('select_one' => 'Select one'); foreach ($provinces as $provinsi) { $options_provinsi[] = array( $provinsi->id => $provinsi->nama, ); } $extra = 'id="provinsi" class="form-control" onChange="loadLocation('provinsi','kota');"'; echo form_dropdown('provinsi', $options_provinsi, 'select_one', $extra); ?> 
This code meet my needs, but because I use array, the dropdown become like this:

dropdown_error

How to set option value without using an array?

Put This :

$extra_string = null;
foreach($extra as $key => $value) {
     $extra_string .= " {$key}='{$value}'"; 
}

And change last line to be:

echo form_dropdown('provinsi', $options_provinsi, 'select_one', $extra_string);

As you can see checking codeigniter docs you will find out that extra param should be string not an array.

  • If it it's an array then form_dropdown() will produce an with the array key as the label.

replace your foreach loop

foreach ($provinces as $provinsi) {
       $options_provinsi[$provinsi->id] = $provinsi->nama;
}

and in view

$extra = null;
echo form_dropdown('provinsi', $options_provinsi, 'select_one', $extra);

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