I've been spent lots of time in this multiple selected array which is hold several values in a multiple dropdown box, and what I want is insert selected values into table.
Let say I been selected 1,2,3 from dropdown box, when I print_r($this->input->post('category'))`, its shown
Array ( [0] => 1 [1] => 2 [2] => 2 )
however when insert into table, its only has the last value is inserted instead of all 3 values.
Here is View to select several values:
$category = array(
'name' => 'category',
'id' => 'category'
);
<select name="category[]" id="<?php echo $category['id'] ?>" multiple="multiple">
<?php
foreach($catOpts as $catOpt)
{
$selected = ($this->input->post('category')==$catOpt->category_name) ? 'selected' : '';
echo '<option value="'.$catOpt->category_id.'" '.$selected.'>'.$catOpt->category_name.'</option>';
}
?>
</select>
In Controller, I pass values to validation and if validation valid,:
$this->form_validation->set_rules('category[]', 'Category', 'required');
if($this->form_validation->run()) { // validation ok
if(!is_null($data = $this->db_model->save_listing(
$this->form_validation->set_value('category[]')
))) { // success
//some message to acknowledge success created.
}
}
Model to insert into table:
function save_listing($category)
{
$data = array(
'category_id' => $category
);
$this->db->insert('listing', $data);
return TRUE;
}
I do not know how to passes all values (array) into controller $this->form_validation->set_value('category[]') and then perform model function save_listing() and save all values into its column in database.
Please help to solve my problem and I has been browse through lots of forum but no luck to get solution.
Thanks.