Jump to content

Loop within loop outputting results once then stopping


msaz87

Recommended Posts

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!

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

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.