• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Store data data from multiple fields

#1
[eluser]Lykos22[/eluser]
I'd like some help please. I have a form in my view where I save (add/edit) values to some features that belong to a specific product. Both features and values are fetched dynamicly, so this is how it looks like:
Code:
<?php echo form_open(); ?>
<?php echo form_hidden('product_id', $product->product_id); ?>
<table class="table">
&lt;?php if(count($features) > 0): foreach($features as $feature): ?&gt;
<tr>
  <td>&lt;?php echo $feature->feature_id . form_hidden('feature_id[]', $feature->feature_id); ?&gt;</td>
  <td>&lt;?php echo form_input('value[]', set_value('value[]', $feature->value)); ?&gt;</td>
</tr>
&lt;?php endforeach; else: ?&gt;
// no features and values yet
So if I dump the $this->input->post() of the form I get the results below as expected:
Code:
Array
(
    [product_id] => 1
    [feature_id] => Array
        (
            [0] => 12
            [1] => 10
            [2] => 8
            [3] => 4
            [4] => 9
            [5] => 11
            [6] => 3
            [7] => 14
            [8] => 13
            [9] => 1
            [10] => 2
        )

    [value] => Array
        (
            [0] => value1
            [1] => value2
            [2] => value3
            [3] => value4
            [4] =>
            [5] =>
            [6] =>
            [7] =>
            [8] =>
            [9] =>
            [10] =>
        )
My question is how can I store these results in my database?? note: I'm using a MY_Model in my application

#2
[eluser]CroNiX[/eluser]
Something like this; loop over one of the arrays and use that key to access the same key in the value array, so you get the matching pair.

Code:
$features = $this->input->post('feature_id', TRUE);
$values = $this->input->post('value', TRUE);

foreach($features as $key => $id)
{
  echo "feature ID = $id, ";
  echo "value = " . $values[$key] . '<br>';
}

should be:
feature ID = 12, value = value1
feature ID = 10, value = value2
...

#3
[eluser]Mangetsu[/eluser]
Something like this can handle both values in single input
Code:
<td>&lt;input type="text" name="feature[&lt;?php echo $feature-&gt;id; ?&gt;]" value="&lt;?php echo $feature-&gt;value; ?&gt;"/&gt;&lt;/td>
then
Code:
$features = $this->input->post('feature', TRUE);

foreach($features as $key => $value)
{
  echo "feature ID = $key ";
  echo "value = " . $value] . '<br>';
}
Didn't test it but should work...

#4
[eluser]Lykos22[/eluser]
Well you can see below in mysql what I want to achieve, either when I add new values, or when editing existing ones:
Code:
--
-- Table structure for table `product_features`
--

CREATE TABLE IF NOT EXISTS `product_features` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `product_id` int(11) unsigned DEFAULT NULL,
  `feature_id` int(11) unsigned DEFAULT NULL,
  `value` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=12 ;

--
-- Dumping data for table `product_features`
--

INSERT INTO `product_features` (`id`, `product_id`, `feature_id`, `value`) VALUES
(1, 1, 1, NULL),
(2, 1, 2, value2),
(3, 1, 3, value3),
(4, 1, 4, value4),
(5, 1, 8, NULL),
(6, 2, 1, another_value1),
(7, 2, 2,  another_value2),
(8, 2, 3, NULL),
(9, 2, 4, another_value4),
(10, 3, 1, NULL),
(11, 3, 2, yet_another_value2);

#5
[eluser]Mangetsu[/eluser]
All you have to do to add new features is

Code:
$features = $this->input->post('feature', TRUE);

foreach($features as $key => $value)
{
$feature=array(
  'product_id'=>$this->input->post('product_id'),
  'feature_id'=>$key,
  'value'=>$value
);

$this->db->insert('product_features',$feature);

}

But this is so simple only if you do insert.. Update gets a bit complicated..Especially if you change feature number, ie. remove one of existing features. I usually do such things by getting all existing features of saved product, check in array if feature exists, if does I do update and remove it from that array. If doesn't I do insert. Remaining features in array are features that exist in DB but not in new data, so i remove them from DB. There's probably better way of doing it, I tend to complicate sometimes, but it's how I've done it in several cases.

#6
[eluser]Lykos22[/eluser]
Actually I add/edit the values of the feature.

Well what I've done so far is I get all features of a saved product, depend on the category the product belongs to (computers have different features than digital cameras). You can also review it here. So when I get the correct features, I can asign them some values, either adding or edititng them.

[quote author="Mangetsu" date="1381572305"]Update gets a bit complicated..Especially if you change feature number, ie. remove one of existing features. I usually do such things by getting all existing features of saved product, check in array if feature exists, if does I do update and remove it from that array. If doesn't I do insert. Remaining features in array are features that exist in DB but not in new data, so i remove them from DB. There's probably better way of doing it, I tend to complicate sometimes, but it's how I've done it in several cases. [/quote]
Could you give me a simple example? I'm not quite sure

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.