Reputation: 5784
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
)
Upvotes: 1
Views: 17350
Reputation: 1
you make it like this:
<code>
<td><?= form_multiselect('categorieen[]', $opties, set_value('categorieen')); ?></td>
</code>
it works for me on my multi select
Upvotes: 0
Reputation: 310
$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:'' )
Upvotes: 0
Reputation: 28763
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>
Upvotes: 4