Page 1 of 1

Help with for loop. I think.

Posted: Tue May 11, 2004 11:20 am
by Yusuf
Okay, here is a brief discription of what I am trying to do.

I have a list of players for a sports league. I am trying to take the players and put them into teams based on age group and skill level.

Here is where my problem is. There needs to be three teams created with 20 players per team. The record set is retreived by order of skill level (i.e. highest skill first and so on down).

My logic is this.

Each player gets assigned to the next team: player 1 to team 1, player 2 to team 2 and player 3 to team 3, player 4 to team 1, mplayer 5 to team2 and player 6 to team 3 and so on and so forth.

So the team lists would look like this:
For team 1 I need players 1, 4, 7, 10, etc...
For team 2 I need players 2, 5, 8, 11, etc...
For team 3 I need players 3, 6, 9, 12, etc...

When I display the info on the screen I can see team 1 created properly. But I do not get team 2 or three printed. But if I change the value for $i in the for statement I can see team 2 and 3 respectively.

I am sorry for the long explaination but I wanted to make sure that what I am doing is completely understood. :-)

So to finish this long winded essay. I seem to be able to get the teams I need by manually entering the $i value for the loop, but I need to have this automated so all three teams will be printed to the screen for me to see.

Any and all help is very much appreciated.

Here is the source code:

Code: Select all

<?php

$result = mysql_query('select id, age, playLevel from players where age between 4 and 6 and gender = "M" and active = 1 order by playLevel DESC');

$getTeamSettings = mysql_query('select totalPlayers, playersPerTeam, numberOfTeams from teams where age = "4-6" and gender = "M"') or die (mysql_error());
$setTeamSettings = mysql_fetch_row($getTeamSettings);


echo 'Total Players Registered: '.$setTeamSettings[0].'<br>';
echo 'Total Players Per Team: '.$setTeamSettings[1].'<br>';
echo 'Number of Teams: '.$setTeamSettings[2].'<br><br><br>';

$numLoop = $setTeamSettings[2] * $setTeamSettings[1];
$setLimit = $setTeamSettings[2];
$setCount = 1;


for($i=1; $i<=$setLimit;$i++){

	$setTeam = 'Team '.$i;
	$setPlayers = '';
	$count = 1;
	$nextPlayer = $i;

	while ($row=mysql_fetch_array($result)) {
	
		if ($numLoop >= $count) {
			
			if ($nextPlayer == $count) {

				if ($count == $i) {			
					$setPlayers = $setTeam. ' = '. $row["id"].',';
				}
				else {
					$setPlayers = $setPlayers.$row["id"].',';
				}//end if 
				
			//if player was added then increment the count
			$nextPlayer = $nextPlayer + $setLimit;

			}//end if add player to list

			$count++;

		}//end if maximum players per team
	
	}//end while

//trying to print the teams to the screen
echo $i.'. '.$setPlayers.'<br>';	

}//end for loop

?>

Posted: Tue May 11, 2004 11:27 am
by JayBird
I think you r problem is the way it is all looped.

You start a loop for team 1, then start a loop for the results from the DB.

When you get to team 2, the result set it now at the end, so it won't itterate through the set again.

You could execute the query for each team, but seems pointless, i think you can set the pointer back to the begining of the results set, but not sure how off the top of my head.

Mark

Posted: Tue May 11, 2004 11:53 am
by Yusuf
Bech100,

You are very correct. If I call the query each time it works great but it is not an optimal solution.

I will look for the answer to your suggestion of pointing back to the beginning of the results set.

Thanks a bunch.

Posted: Tue May 11, 2004 5:50 pm
by Weirdan
Yusuf wrote: I will look for the answer to your suggestion of pointing back to the beginning of the results set.
[php_man]mysql_data_seek[/php_man]:

Code: Select all

//....
mysql_data_seek($result,0);

That's it

Posted: Tue May 11, 2004 8:39 pm
by Yusuf
Weirdan,

That did it.

Thank you very much.