Set multiple values as selected in form_multiselec

2019-08-08 18:36发布

I wonder if there's a way of setting multiple (say 3) values 'selected' in form_multiselect(). I got it working with only 1 value using key($selectie) where $selectie is my query to get the values form the database for a specific company id.

My model query looks like this:

function getselection($id2)
{
    $this->db->from('bedrijfcategorieen');
    $this->db->join('categorieen', 'bedrijfcategorieen.idcategorieen = categorieen.idcategorieen');
    $this->db->where('bedrijfcategorieen.idbedrijven', $id2);
    $query = $this->db->get();

    return $query->result_array();
}

My controller:

    $data['selected'] = $this->members_model->getselection($id2);

My views:

<?php
    foreach($selected as $row){
        $selectie[$row['idcategorieen']] = $row['Categorie'];
    }
    echo '<pre>';
    print_r($selectie);
    echo '</pre>';
?>

<tr>
<td><?= form_label('Categorieen'); ?></td>
<td><?= form_multiselect('categorieen[]', $opties, key($selectie)); ?></td>
</tr>

print_r($selectie); produces:

Array
(
    [11] => Webdesign
    [12] => Vlaggen
    [13] => Auto-s
)

3条回答
Fickle 薄情
2楼-- · 2019-08-08 18:45
  $list= Array(
    [11] => Webdesign
    [12] => Vlaggen
    [13] => Auto-s);

 $a=$this->input->post('name');       
    echo form_multiselect('name[]', $list,($a)?$a:'');

or

$a=$this->input->post('emp_name');
echo form_dropdown('emp_name[]', $employee, ($a)?$a:'' )
查看更多
Juvenile、少年°
3楼-- · 2019-08-08 18:59

Change your view like this

<?php
    foreach($selected as $row){
        $selectie[] = $row['idcategorieen'];
    }
?>

<tr>
   <td><?= form_label('Categorieen'); ?></td>
    <td><?= form_multiselect('categorieen[]', $opties, $selectie); ?></td>
</tr>
查看更多
4楼-- · 2019-08-08 19:01

you make it like this:

<code>
<td><?= form_multiselect('categorieen[]', $opties, set_value('categorieen')); ?></td>
</code>

it works for me on my multi select

查看更多
登录 后发表回答