ale1981 Posted October 27, 2015 Share Posted October 27, 2015 It's late in the day and I can't figure this one out, I know it's going to be pretty easy! I have a form which sends the following array; array:2 [▼"names" => array:3 [▼0 => "Default"1 => "100ml"2 => "150ml;"]"prices" => array:3 [▼0 => "12.99"1 => "9.99"2 => "14.99"] I want to loop the array and do an insert using this array like so, the index from "names" and "prices"; INSERT INTO DB VALUES('Default',12.99); INSERT INTO DB VALUES('100ml',9.99); INSERT INTO DB VALUES('150ml',14.99); Thanks Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted October 27, 2015 Solution Share Posted October 27, 2015 Use a foreach loop to iterative over the names array, using the array key to get the corresponding value in the prices array // loop over the names array foreach($array['names'] as $key => $name) { // use the array key to reference the corresponding value in the prices array $price = $array['prices'][$key]; // do insert query } Change $array to be your variable that contain the names and prices array Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 27, 2015 Share Posted October 27, 2015 I would like to see your form. Quote Link to comment Share on other sites More sharing options...
ale1981 Posted October 27, 2015 Author Share Posted October 27, 2015 Use a foreach loop to iterative over the names array, using the array key to get the corresponding value in the prices array // loop over the names array foreach($array['names'] as $key => $name) { // use the array key to reference the corresponding value in the prices array $price = $array['prices'][$key]; // do insert query } Change $array to be your variable that contain the names and prices array Thanks Ch0cu3r that worked, I knew there would be a simple answer! I would like to see your form. I have 2 input fields that use the same variable array, e.g. <input type="text" name="productVariations[name][]" /> <input type="text" name="productVariations[price][]" /> Is there a different way? Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 27, 2015 Share Posted October 27, 2015 (edited) I have 2 input fields that use the same variable array, e.g. <input type="text" name="productVariations[name][]" /> <input type="text" name="productVariations[price][]" /> Is there a different way? There is always a different way, the question is if there is a better way or not - and in this case there is. I was actually going to speak to the same problem that I'm sure benanamen was going to address, but decided against it once Ch0cu3r responded. You should not have data where there is an assumption of correlation based on position. This is bad form and will eventually lead to bugs. Instead, the correlated data should be structured such that the correlation is specifically defined. In this case you have pairs of fields that should be correlated within the structure. Using a multidimensional array was the right choice - but I would swap the keys so that the records are logically structured. But, instead of "[]" you will need to define the key. E.g. <input type="text" name="productVariations[0][name] /> <input type="text" name="productVariations[0][price]" /> <input type="text" name="productVariations[1][name] /> <input type="text" name="productVariations[1][price]" /> <input type="text" name="productVariations[2][name] /> <input type="text" name="productVariations[2][price]" /> I'm sure I've left off some content that goes between the fields and, ideally, the output would be generated within a loop. using that structure, the array of the post data would look like this productVariations array ( 0 => ("name" => "Default", "price" => "12.99"), 1 => ("name" => "100ml", "price" => "9.99"), 2 => ("name" => "150ml", "price" => "14.99") ) Then the code to process the data could look like this foreach($productVariationsAry as $record) { $name = $record['name']; $price = $record['price']; // do insert query } FYI: It would also be more efficient to create ONE insert query to insert all records at one. Edited October 27, 2015 by Psycho 2 Quote Link to comment Share on other sites More sharing options...
ale1981 Posted October 28, 2015 Author Share Posted October 28, 2015 There is always a different way, the question is if there is a better way or not - and in this case there is. I was actually going to speak to the same problem that I'm sure benanamen was going to address, but decided against it once Ch0cu3r responded. You should not have data where there is an assumption of correlation based on position. This is bad form and will eventually lead to bugs. Instead, the correlated data should be structured such that the correlation is specifically defined. In this case you have pairs of fields that should be correlated within the structure. Using a multidimensional array was the right choice - but I would swap the keys so that the records are logically structured. But, instead of "[]" you will need to define the key. E.g. <input type="text" name="productVariations[0][name] /> <input type="text" name="productVariations[0][price]" /> <input type="text" name="productVariations[1][name] /> <input type="text" name="productVariations[1][price]" /> <input type="text" name="productVariations[2][name] /> <input type="text" name="productVariations[2][price]" /> I'm sure I've left off some content that goes between the fields and, ideally, the output would be generated within a loop. using that structure, the array of the post data would look like this productVariations array ( 0 => ("name" => "Default", "price" => "12.99"), 1 => ("name" => "100ml", "price" => "9.99"), 2 => ("name" => "150ml", "price" => "14.99") ) Then the code to process the data could look like this foreach($productVariationsAry as $record) { $name = $record['name']; $price = $record['price']; // do insert query } FYI: It would also be more efficient to create ONE insert query to insert all records at one. Thanks for the detailed response. I am adding the fields dynamically so I do not know how many the user is going to have, I guess I could use a bit of JS to determine this. Yes the SQL query will be one query, I used the above as an example of what I needed. Thanks for all responses. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.