I am using jQuery plugin for mutliselect options.Everything is working fine but only one issue is it is keep selected at least one item.
If I select multiple items it works but than when I am trying to remove all selected items and save the form to store data in database it is revert by keeping at least one item selected.
EDIT: I have added full code what I am trying to do with. The issue is not keeping properly selected items and finally keep selected one item.
Controller
public function general()
{
$this->data['title'] = admin_page_heading('glyphicon glyphicon-cog', 'title_settings');
// check if form submited
if(!empty($_POST)):
// get form fields name and value automatic
foreach($_POST as $key => $value):
// convert result in array
$data[] = $key;
endforeach;
endif;
//echo '<pre>', print_r($data), '</pre>';
$rules = $this->settings_model->rules;
$this->form_validation->set_rules($rules);
// Process the form
if($this->form_validation->run() === TRUE):
$data = $this->settings_model->array_from_post($data);
// add or update value
foreach($data as $name => $value):
// check if option is in record
if(get_option($name)):
// update the optoin
update_option($name, $value);
else:
// add new row if option is not in record
add_option($name, $value);
endif;
endforeach;
endif;
// load general settings view
$this->load->view('settings/general', $this->data);
}
Add Option
function add_option($name,$value)
{
$CI =& get_instance();
$CI->load->database();
$query=$CI->db->select('*')->from('site_options')->where('option_name',$name)->get();
//option already exists
if($query->num_rows() > 0)
return false;
$data_type='text';
if(is_array($value))
{
$data_type='array';
$value=serialize($value);
}
elseif(is_object($value))
{
$data_type='object';
$value=serialize($value);
}
$data=array(
'option_name'=>$name,
'option_value'=>$value,
'option_type'=>$data_type,
);
$CI->db->insert('site_options',$data);
}
Update Option
function update_option($name,$value)
{
$CI =& get_instance();
$CI->load->database();
$data_type='text';
if(is_array($value))
{
$data_type='array';
$value=serialize($value);
}
elseif(is_object($value))
{
$data_type='object';
$value=serialize($value);
}
$data=array(
'option_name'=>$name,
'option_value'=>$value,
'option_type'=>$data_type,
);
$query=$CI->db->select('*')->from('site_options')->where('option_name',$name)->get();
//if option already exists then update else insert new
if($query->num_rows() < 1) return $CI->db->insert('site_options',$data);
else return $CI->db->update('site_options',$data,array('option_name'=>$name));
}
Function to populate Multiselect
function get_multiselect($name, $items = array(), $id=false)
{
echo '<select multiple="multiple" id="'.$id.'" name="'.$name.'[]">';
$item = $items;
$selects = get_option($name);
for($i=0; $i<count($item); $i++):
$selected = (in_array($item[$i], $selects) ? 'selected="selected"' : NULL);
echo '<option value="'.$item[$i].'" '.$selected.'>'.$item[$i].'</option>';
endfor;
echo '</select>';
}
I found this is happening because of serialized data which needs at least one item in array
If I removed array items from database it is giving an error. So just wonder how to check if there is not item selected than insert NULL or blank (string).
Might be database does not have the null allowed in the field.. You can have one value as empty
<option value=""> ----Select Options ----- </option>
Then remaining the code for values .. Try and revert lets check