Jump to content

Two lists for one query...


Jim R

Recommended Posts

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.  

 

post-44159-0-79325900-1514382435_thumb.png

 

 

Link to comment
Share on other sites

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')

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.