Page 1 of 1
Need help looping through an array
Posted: Tue Feb 22, 2011 7:32 pm
by BelowZero
I'm trying to query my database by looping through an array to pick a particular row. I'm not sure which command I should use.
If I assign an array to pick from:
$gamenumber = range(1,16);
how do I loop through the array, inserting a new $gamenumber for "schedule.game_id" each time?
i.e.
$result = mysql_query("SELECT * FROM schedule, teams WHERE schedule.week_id = '1' AND schedule.game_id = $gamenumber" or die (mysql_error());
Thanks for any help!
Re: Need help looping through an array
Posted: Tue Feb 22, 2011 7:46 pm
by califdon
I'm not really sure I understand what you want to do. I would think that you would just use one query and use GROUP BY or just ORDER BY to get all the records, the way you want them. If you have a different need that I'm missing, you need to explain what it is.
Re: Need help looping through an array
Posted: Tue Feb 22, 2011 11:11 pm
by BelowZero
Perhaps it will be easier to see what I'm trying to accomplish here:
http://www.beat-the-spread.net/2010week1.html
Last year I used Excel to produce the results pages and included them in the website.
For each week there are up to 16 games, but as you can see, each game needs 2 rows to display the results. One for the home team info and one for the away team info.
In my database all the results are stored in 1 row of the table with a unique id, a week_id and a game_id, plus the other info. My thinking was that I could query the database for game_id=1, print 2 rows of results, then loop to the next game_id, print, and so on.
Does that make sense?
Re: Need help looping through an array
Posted: Wed Feb 23, 2011 2:31 pm
by califdon
What you need to learn is what queries can do for you. Queries are the heart and blood of relational databases and can do most of the kinds of data grouping in the query. It is rare that you would need to loop through the results of a query. Read some tutorials on the use of GROUP BY in an SQL statement. I don't have time right now to analyze your particular requirements, and I see that they are a bit more complicated than a simple query, but I am sure you can get the results you want without resorting to looping through a bunch of queries with different WHERE criteria.