![]() |
Form With Multiple Fields [Array] Query - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Form With Multiple Fields [Array] Query (/showthread.php?tid=65676) |
Form With Multiple Fields [Array] Query - MoFish - 07-11-2016 Hi All, I have some form fields which can be repeated multiple multiple times within my form. For example, the same two fields, which are repeating 5 times within my form. I'm a little unclear on the best way to process multiple form fields data so they can be saved back into the DB. If anyone could shed anylight on this? it would be much appriciated. My form has a couple fields. View: PHP Code: $data = array('name'=>'keyword_meta_title[]', 'id' => 'keyword_meta_title'); I'm trying to loop around the form fields and create a $data array with the relevant information for each of the fields which i will then pass into my update statement. Controller PHP Code: if($this->form_validation->run() === TRUE){ Thanks, MoFish RE: Form With Multiple Fields [Array] Query - mwhitney - 07-11-2016 If you have multiple title/description pairs which you want to loop through and pass as an array to your update_keywords method, you probably need to do something like this: PHP Code: // Note: you should probably make sure the $titles and $descriptions contain the As a further optimization, I would normally do the update outside of the loop, but that requires creating an array of entries that can be passed to a batch update method in the model, which requires some more significant changes, including a method in the model to handle a batch update. RE: Form With Multiple Fields [Array] Query - MoFish - 07-11-2016 Thank you for the reply mwhitney, i'll give this a go later on tonight and let you know how I get on. RE: Form With Multiple Fields [Array] Query - MoFish - 07-11-2016 Hi, It's working to some extent, but i dont think i have the correct ID for each individual form field im retreiving from the db. Is there a way I can pass the ID from the view back into my controller? e.g 'name'=>'keyword_meta_title[][ID]? Then in my controller use this ID inside my update statement? See my code below, and a comment on the area which i beleive is the issue. PHP Code: if($this->form_validation->run() === TRUE){ RE: Form With Multiple Fields [Array] Query - MoFish - 07-12-2016 Hi, I used a hidden field to pass the ID back and this worked OK. Not the ideal solution but works fine. @mwhitney, i'm always for futher optimization! If you wouldn't mind, would you be able to give me an example of how I would go about creating an array of entries which i can then pass into the batch_update inside my model? Regards, MoFish RE: Form With Multiple Fields [Array] Query - mwhitney - 07-12-2016 To do the update as a batch, you would do something like this: PHP Code: $data = array(); This assumes that $id is an array of ID values which can be used to identify the data to be updated in the batch update. Essentially, as you indicated, this would be a hidden field using an array for the name just like the other fields in the form. As you can see, $data is defined as an empty array before the for loop starts, then each iteration of the loop appends an array to $data (rather than redefining $data), so you have a 2-dimensional array, similar to the output of result_array() when you're working with a database query. Then the batch_update_keywords() method in the model just has to perform a batch update, which is relatively easy using query builder's update_batch() method (especially since we've just created $data in the format required by this method). Query builder's update_batch() just needs the table name, $data, and the name of the key ('id' in the example above). RE: Form With Multiple Fields [Array] Query - MoFish - 07-13-2016 Thank you very much mwhitney for taking the time to explain. It worked a treat and is much cleaner than before! thanks again! MoFish |