Store data data from multiple fields - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Store data data from multiple fields (/showthread.php?tid=59479) |
Store data data from multiple fields - El Forum - 10-10-2013 [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(); ?> Code: Array Store data data from multiple fields - El Forum - 10-10-2013 [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); should be: feature ID = 12, value = value1 feature ID = 10, value = value2 ... Store data data from multiple fields - El Forum - 10-11-2013 [eluser]Mangetsu[/eluser] Something like this can handle both values in single input Code: <td><input type="text" name="feature[<?php echo $feature->id; ?>]" value="<?php echo $feature->value; ?>"/></td> Code: $features = $this->input->post('feature', TRUE); Store data data from multiple fields - El Forum - 10-12-2013 [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: -- Store data data from multiple fields - El Forum - 10-12-2013 [eluser]Mangetsu[/eluser] All you have to do to add new features is Code: $features = $this->input->post('feature', TRUE); 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. Store data data from multiple fields - El Forum - 10-12-2013 [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 Store data data from multiple fields - El Forum - 10-12-2013 [eluser]Mangetsu[/eluser] Something like this Code: //MODEL |