Welcome Guest, Not a member yet? Register   Sign In
Store data data from multiple fields
#7

[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));
}
}


Messages In This Thread
Store data data from multiple fields - by El Forum - 10-10-2013, 04:47 AM
Store data data from multiple fields - by El Forum - 10-10-2013, 09:17 AM
Store data data from multiple fields - by El Forum - 10-11-2013, 02:27 AM
Store data data from multiple fields - by El Forum - 10-12-2013, 02:00 AM
Store data data from multiple fields - by El Forum - 10-12-2013, 03:05 AM
Store data data from multiple fields - by El Forum - 10-12-2013, 03:31 AM
Store data data from multiple fields - by El Forum - 10-12-2013, 04:05 AM



Theme © iAndrew 2016 - Forum software by © MyBB