msaz87 Posted December 8, 2009 Share Posted December 8, 2009 Hey all, I'm trying to execute a loop within a loop and it's only outputting the second loop's results once. To summarize what it's doing, the first loop finds all the teams within this schedule, then the second loop finds all the various times the teams might play and counts how many times the team plays at that time... but the output looks like this: 281 (team) -- 0830 (time) - 1 (count) -- 0920 - 1 -- 1010 - 0 -- 1100 - 0 -- 1150 - 0 279 283 289 278 282 277 280 284 Here's the code... <?php $team_list_query = " SELECT DISTINCT team FROM schedules WHERE league_date = '$league_date' AND league = '$league_selection'"; $team_list_results = mysql_query($team_list_query) or die(mysql_error()); $time_list_query = " SELECT DISTINCT gametime FROM schedules WHERE league_date = '$league_date' AND league = '$league_selection'"; $time_list_results = mysql_query($time_list_query) or die(mysql_error()); while($row = mysql_fetch_array($team_list_results)){ $team = $row['team']; echo $team."<br/>"; while($row = mysql_fetch_array($time_list_results)){ $time = $row['gametime']; $error_check_query = " SELECT * FROM schedules WHERE league_date = '$league_date' AND league = '$league_selection' AND gametime = '$time' AND team = '$team'"; $error_check_results = mysql_query($error_check_query) or die(mysql_error()); $error_check = mysql_num_rows($error_check_results); echo "-- ".$time." - ".$error_check."<br/>"; } } Any help is greatly appreciated -- thanks! Quote Link to comment https://forums.phpfreaks.com/topic/184332-loop-within-loop-outputting-results-once-then-stopping/ Share on other sites More sharing options...
cags Posted December 8, 2009 Share Posted December 8, 2009 The first time you run through the inner loop it will move the pointer to the end of the dataset $time_list_results, so the second time through the outter loop the inner loop won't even be entered because the first call to mysql_fetch_array will return FALSE. I've never actually used the function so I could be wrong, but I think what your after may be mysql_data_seek (that is of course assuming it allows you to track backwards). Quote Link to comment https://forums.phpfreaks.com/topic/184332-loop-within-loop-outputting-results-once-then-stopping/#findComment-973166 Share on other sites More sharing options...
msaz87 Posted December 8, 2009 Author Share Posted December 8, 2009 I repositioned one of the queries inside the first loop... so it's not the most efficient method as it's re-running that query each time, but it works.. Quote Link to comment https://forums.phpfreaks.com/topic/184332-loop-within-loop-outputting-results-once-then-stopping/#findComment-973262 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.