Jim R Posted December 27, 2017 Share Posted December 27, 2017 I guess the 'easiest' question, is can there be two "while" loops with a query? I've looked that up and haven't really found a definite answer. I'm not looking for code to be written yet, just a push in a certain direction. Here is what I'm looking for: I'm separately wanting player names and if there are any events players play in. Games are not treated as events, so not every row deals with an event. Most will be games, especially this time of year. Right now I'm adding a row or two each game about individual players. The last "event" was in October. The next "event" won't be until April. In the spring and summer, there could be as many as 30-40 rows for a single event. So I want two lists on this one page view: The most recent events information is posted on. The most recent players information is posted on. The screenshot below is the current list, grouped by player name, listed alphabetically. Above it, I'd like to have from the same data table a list of the most recent events. Is it just easier to do it with a separate query? I might be complicating this, but I was once told on here not to query the same table on the same file. Quote Link to comment https://forums.phpfreaks.com/topic/306004-two-lists-for-one-query/ Share on other sites More sharing options...
ginerjm Posted December 27, 2017 Share Posted December 27, 2017 Instead of doing a while loop that does a Fetch(), do a standalone FetchAll of the query results. That will move the results from a resource to a true array type and then you can do as many loops on that data as you want. Be sure to specify an Associated type of retrieval when you do the fetchall. (in pdo, that would be 'PDO::FETCH_ASSOC') Quote Link to comment https://forums.phpfreaks.com/topic/306004-two-lists-for-one-query/#findComment-1554924 Share on other sites More sharing options...
Jim R Posted December 27, 2017 Author Share Posted December 27, 2017 I just added a second query, and it worked. Is there any reason why I shouldn't have two queries from the data table on the same page file? It seemed like awhile ago (maybe even years ago) I was told not to do that. Quote Link to comment https://forums.phpfreaks.com/topic/306004-two-lists-for-one-query/#findComment-1554925 Share on other sites More sharing options...
ginerjm Posted December 27, 2017 Share Posted December 27, 2017 Running a second query to do your second task makes perfect sense of course. But - if you want to reduce resource usage and use the data in two different ways without re-generating the query results, what I gave you works just fine - assuming that you have made the proper query in the first place. Quote Link to comment https://forums.phpfreaks.com/topic/306004-two-lists-for-one-query/#findComment-1554926 Share on other sites More sharing options...
Psycho Posted December 28, 2017 Share Posted December 28, 2017 I just added a second query, and it worked. Is there any reason why I shouldn't have two queries from the data table on the same page file? It seemed like awhile ago (maybe even years ago) I was told not to do that. If the two queries are supposed to return a different result set, it would make sense. Your explanation was a little hard to follow. Some code on what you had/have might have been helpful. Whoever gave you that former advice either didn't know what they were talking about or they were talking about something quite different than what I think you were stating and you misinterpreted them. Quote Link to comment https://forums.phpfreaks.com/topic/306004-two-lists-for-one-query/#findComment-1554942 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.