craygo Posted April 26, 2006 Share Posted April 26, 2006 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 playerswe play for 20 weeksAll players are in a MySql database with a unique ID for eachWhat 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.ThanksRay Quote Link to comment https://forums.phpfreaks.com/topic/8466-random-assignment/ Share on other sites More sharing options...
wisewood Posted April 26, 2006 Share Posted April 26, 2006 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.[code]<?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++; } } }}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/8466-random-assignment/#findComment-30964 Share on other sites More sharing options...
craygo Posted April 26, 2006 Author Share Posted April 26, 2006 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.[code]<?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 weeksfor($i=1;$i<$weeks;$i++){$p1 = rand((($players/2)+1), $players); if($i !== $p1){ echo "$i vs $p1<br>"; }}?>[/code]Which returns[code]1 vs 142 vs 183 vs 144 vs 145 vs 146 vs 137 vs 118 vs 199 vs 2010 vs 13[/code]Obviously player 14 cannot play 4 people in one weekI 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 Quote Link to comment https://forums.phpfreaks.com/topic/8466-random-assignment/#findComment-31080 Share on other sites More sharing options...
craygo Posted April 27, 2006 Author Share Posted April 27, 2006 bump it up fellas!! :) Quote Link to comment https://forums.phpfreaks.com/topic/8466-random-assignment/#findComment-31285 Share on other sites More sharing options...
ober Posted April 27, 2006 Share Posted April 27, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/8466-random-assignment/#findComment-31291 Share on other sites More sharing options...
craygo Posted April 27, 2006 Author Share Posted April 27, 2006 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 postRay Quote Link to comment https://forums.phpfreaks.com/topic/8466-random-assignment/#findComment-31306 Share on other sites More sharing options...
wisewood Posted April 27, 2006 Share Posted April 27, 2006 It's definately possible. Just takes some thinking about because of all the ifs and maths involved. Quote Link to comment https://forums.phpfreaks.com/topic/8466-random-assignment/#findComment-31348 Share on other sites More sharing options...
craygo Posted April 27, 2006 Author Share Posted April 27, 2006 If anyone has some code I would appreciate it.Ray Quote Link to comment https://forums.phpfreaks.com/topic/8466-random-assignment/#findComment-31408 Share on other sites More sharing options...
craygo Posted April 28, 2006 Author Share Posted April 28, 2006 It's a new day here in PHPville Morning to everyone!!!Ray Quote Link to comment https://forums.phpfreaks.com/topic/8466-random-assignment/#findComment-31631 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.