Jump to content


Photo

Random Assignment


  • Please log in to reply
8 replies to this topic

#1 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 26 April 2006 - 03:03 PM

I am trying to create a php application for my golf league. What I need to do is setup matches. Here is a senario:

I have 20 players
we play for 20 weeks
All players are in a MySql database with a unique ID for each
What I want to do is have php randomize matches for the players for all 20 weeks but not play the same person if at all possible. Obviously if it were 10 players for 20 weeks then playing someone twice would be fine but not till he has played everyone first.

Thanks

Ray

#2 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 26 April 2006 - 03:18 PM

basically...

Select all players from the golf_league table, and put them in order by their names.
Then, for each golfer, select all players from the golf_league in random order.

If the golfer they're playing against has the same name as them, leave them off the list, else display the name of their opponent and the week number.

<?php


$strSQL = "SELECT * FROM golf_league ORDER BY golfers_name ASC";
if ($result = mysql_query($strSQL) or die ("Invalid query")) 
{
    $num = mysql_num_rows($result);
    for($i=0;$i<$num;$i++)
    {
        $golfers_name = mysql_result($result,$i,"golfers_name");

    echo "$golfers_name will play against:<br>";

    $strSQL2 = "SELECT * FROM golf_league ORDER BY RAND()";
    if ($result2 = mysql_query($strSQL2) or die ("Invalid query")) 
    {
        $num2 = mysql_num_rows($result2);

        $week = 1;
        for($i2=0;$i2<$num2;$i2++)
        {    
            $golfers_name2 = mysql_result($result,$i,"golfers_name");
        
        if($golfers_name2!=="$golfers_name"){
        echo "Week $week. $golfers_name<br>";}
        $week++;
        }
    }
    }
}
?>

wisewood: proven fact, I am both wise, and wooden.

#3 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 26 April 2006 - 08:01 PM

ok I got the idea you have there, but I still got a problem. With what you have the randow number is picked but it can also be repeated. So I am coming up with things like this.

My players all have unique ids in order so I only have to use the database to get the number of players. I have this code based a little on wisewood's post.

<?php
// will get this number from database
$players = '20';
// add one to the weeks because I want to start at 1 and go the desired weeks
$weeks = 10+1;
// loop through the desired weeks
for($i=1;$i<$weeks;$i++){
$p1 = rand((($players/2)+1), $players);
  if($i !== $p1){
  echo "$i vs $p1<br>";
  }
}
?>

Which returns
1 vs 14
2 vs 18
3 vs 14
4 vs 14
5 vs 14
6 vs 13
7 vs 11
8 vs 19
9 vs 20
10 vs 13

Obviously player 14 cannot play 4 people in one week

I want it to look like this but without all the other stuff. I just need the 1 vs 2 part
[a href=\"http://eagolf.theelders.net/2006/schedule.pdf\" target=\"_blank\"]http://eagolf.theelders.net/2006/schedule.pdf[/a]

Any thoughts??

Ray

#4 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 27 April 2006 - 12:06 PM

bump it up fellas!! :)

#5 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 27 April 2006 - 12:17 PM

Put the ones it has come up with in an array. If it comes up with that same number again, redo your randomizing before moving on to the next match. You can use in_array() to see if the new random number is in your "used" array.

Info: PHP Manual


#6 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 27 April 2006 - 12:58 PM

will it be a problem to have both numbers in the array. Reason I am asking is I eventually need to insert these into a table, so I want to have it in a format I can insert.

I think this will be tough to do, because eventually I may use this elsewhere and if there is a case where I only have 10 teams but 20 weeks of golf, then the teams will play each other more than once. So the no repeat will have to cancel out itself once all teams have been played. I am not even sure this is posible with php but I am trying like hell to do it.

If you want table structure I have right now i will post

Ray

#7 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 27 April 2006 - 03:04 PM

It's definately possible. Just takes some thinking about because of all the ifs and maths involved.
wisewood: proven fact, I am both wise, and wooden.

#8 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 27 April 2006 - 06:50 PM

If anyone has some code I would appreciate it.

Ray

#9 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 28 April 2006 - 12:01 PM

It's a new day here in PHPville Morning to everyone!!!

Ray




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users