Scooby08 Posted February 17, 2011 Share Posted February 17, 2011 I have a query like so: SELECT * FROM table WHERE event_id IN (772280,772280) Is there any way to force that query to loop twice and return two results? I'm looping the $result like so and need two results looped: foreach($result as $r){ // results } I doubt there's a way to do this, but I figured I'd try throwing it out there and see.. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/227962-is-there-any-way-to-force-a-query-to-loop-unique-ids/ Share on other sites More sharing options...
requinix Posted February 17, 2011 Share Posted February 17, 2011 Why not just run the code twice? Avoiding copy and paste, foreach ($results as $r) { for ($i = 1; $i // code } } Quote Link to comment https://forums.phpfreaks.com/topic/227962-is-there-any-way-to-force-a-query-to-loop-unique-ids/#findComment-1175528 Share on other sites More sharing options...
Scooby08 Posted February 17, 2011 Author Share Posted February 17, 2011 Ahh I thought that was gonna work! I guess I left some details out.. There's another issue then.. Not all id's are going to be duplicates.. Take this query for example: SELECT * FROM table WHERE event_id IN (772280,772280,772457,772458,772453,772454) I ran your code and it loops all of them 6 times each.. I just need one loop for each id.. Thanks a ton for the suggestion requinix! Quote Link to comment https://forums.phpfreaks.com/topic/227962-is-there-any-way-to-force-a-query-to-loop-unique-ids/#findComment-1175534 Share on other sites More sharing options...
kickstart Posted February 17, 2011 Share Posted February 17, 2011 Hi I can't think of an elegant way of doing it. Best I can come up with is to create a tempory table, insert all the keys within the IN clause and then use an INNER JOIN between the temp table and the actual table. Bit of a faff though. CREATE TEMPORARY TABLE KeyList (Id int(11) NOT NULL AUTO_INCREMENT, RealField int(11) NOT NULL, PRIMARY KEY (`Id`), KEY `RealField` (`RealField`)); INSERT INTO KeyList (Id, RealField ) VALUES (NULL,772280),(NULL,772280),(NULL,772457),(NULL,772458),(NULL,772453),(NULL,772454); SELECT * FROM table INNER JOIN KeyList ON table.event_id = KeyList.Realfield; All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/227962-is-there-any-way-to-force-a-query-to-loop-unique-ids/#findComment-1175606 Share on other sites More sharing options...
fenway Posted February 17, 2011 Share Posted February 17, 2011 What are you trying to achieve here? Quote Link to comment https://forums.phpfreaks.com/topic/227962-is-there-any-way-to-force-a-query-to-loop-unique-ids/#findComment-1175776 Share on other sites More sharing options...
Scooby08 Posted February 17, 2011 Author Share Posted February 17, 2011 Sorta hard to explain what I'm doin but I'll try.. I have a bunch of checkboxes with the value format of id_type.. (i.e. 772280_one) Each id (or group of checkboxes) has 3 different choices for example.. Like so: <input type="checkbox" value="772280_one" /> <input type="checkbox" value="772280_two" /> <input type="checkbox" value="772280_three" /> <input type="checkbox" value="772281_one" /> <input type="checkbox" value="772281_two" /> <input type="checkbox" value="772281_three" /> <input type="checkbox" value="772282_one" /> <input type="checkbox" value="772282_two" /> <input type="checkbox" value="772282_three" /> .....many many more checkboxes with different id's below here as well..... What I was trying to avoid was running a query for each checkbox checked like so: foreach($result as $r){ // run my query for each choice here that selects the data for that rows id // now create a string for the insert here with the new data selected from the rows id } // then finally insert the data into the next table I was trying to just run a single query that selects all the info at once.. And then run another single query to insert all the new data at once.. When I create my string of id's and run them trough the loop it only loops duplicates once.. The example I was trying to avoid is working perfectly except it runs a query for each checkbox checked.. I guess it's ok.. I'm rather new to running queries and I was just trying to optimize them as best I can because I'm planning on having thousands of records to query here.. Who knows, maybe it'll be just fine running a query for each id and then doing one query at the end to insert all that data into the new table.. I'd say at max there could be like 25 ids to loop and query at a time.. What does anybody think? Good to go? or can it be set up better? Thanks!! I hope that explains a bit more about what I'm trying to accomplish.. Quote Link to comment https://forums.phpfreaks.com/topic/227962-is-there-any-way-to-force-a-query-to-loop-unique-ids/#findComment-1175810 Share on other sites More sharing options...
fenway Posted February 17, 2011 Share Posted February 17, 2011 Why not simply have them share a name attribute? Quote Link to comment https://forums.phpfreaks.com/topic/227962-is-there-any-way-to-force-a-query-to-loop-unique-ids/#findComment-1175846 Share on other sites More sharing options...
Pikachu2000 Posted February 17, 2011 Share Posted February 17, 2011 Why not simply have them share a name attribute? Which it appears that none of them even have at the moment . . . Quote Link to comment https://forums.phpfreaks.com/topic/227962-is-there-any-way-to-force-a-query-to-loop-unique-ids/#findComment-1175876 Share on other sites More sharing options...
Scooby08 Posted February 18, 2011 Author Share Posted February 18, 2011 Can you give me an example of how that would help? Quote Link to comment https://forums.phpfreaks.com/topic/227962-is-there-any-way-to-force-a-query-to-loop-unique-ids/#findComment-1175956 Share on other sites More sharing options...
kickstart Posted February 18, 2011 Share Posted February 18, 2011 What does anybody think? Good to go? or can it be set up better? Try using my temp table suggestion. Failing that have a table of options (pretty static) listing the basic value and the full value (ie, 772280_one, 772280_two, 772280_three). So 772280 has 3 (or however many) rows on this table. Have your form return the full valve then JOIN to your main table based on the basic value and check the full value in your WHERE clause. Ie OptionsTable FullValue, BasicValue '772280_one', 772280 '772280_two', 772280 '772280_three', 772280 SELECT * FROM table INNER JOIN OptionsTable ON table.event_id = OptionsTable.BasicValue WHERE OptionsTable.FullValue IN ('772280_one','772280_two','772280_three'); All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/227962-is-there-any-way-to-force-a-query-to-loop-unique-ids/#findComment-1176141 Share on other sites More sharing options...
fenway Posted February 18, 2011 Share Posted February 18, 2011 Can you give me an example of how that would help? Because then you'll get them as a collection. Quote Link to comment https://forums.phpfreaks.com/topic/227962-is-there-any-way-to-force-a-query-to-loop-unique-ids/#findComment-1176447 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.