[eluser]Mangetsu[/eluser]
Something like this
Code:
//MODEL
function get_product_features($product_id){
$this->db->from('product_features');
$this->db->where('product_id'.$product_id);
$query=$this->db->query();
return $query->result_array();
}
//CONTROLER
$product_id=$this->input->post('product_id');
$current_features=$this->your_model->get_product_features($product_id);
//Now you have current features in db and you need some way of compare
//this can be done by using some array search option if you want but this way worked for me
//this is my way, i create simple array where feature_id is key and id is value
$foo=array();
foreach( current_features as $cf){
$foo[$cf['feature_id']]=$cf['id'];
}
// we dont need old array any more
unset(current_features);
//and now
$features = $this->input->post('feature', TRUE);
foreach($features as $key => $value)
{
$feature=array(
'product_id'=>product_id,
'feature_id'=>$key,
'value'=>$value
);
//now we do comparison, for features we got from post we check if there's index in $foo array with id of that feature
//if there is, we do update (table id is value under that key), if not we do insert
if(isset($foo[key]){
$this->db->update('product_features', $feature, array('id' => $foo[key]));
//remove feature from $foo array so in the end we get features we need to delete
unset($foo[key]);
}
else
$this->db->insert('product_features',$feature);
}
// in the end if $foo is not empty(in case we have some feature in DB and not i new post) we do cleanup
if(!empty($foo)){
foreach($foo as $f){
$this->db->delete('product_features', array('id' => $f));
}
}