问题
I have a table players with N users who sing up! I need to create random unique team from that list!
Great to @geomagas currently i use:
$nteams=$_POST['teams'];
$nbase=$_POST['base'];
$npivots=$_POST['pivots'];
$allplayers=$nteams*($nbase+$npivots);
require_once "connect_to_mysql.php";
$sqlCommand = "SELECT id FROM players ORDER BY RAND() LIMIT $allplayers";
$query = mysql_query($sqlCommand) or die (mysql_error());
if(mysql_num_rows($query)<$allplayers) // sanity
die('Not enough players!');
else
for($team=1;$team<=$nteams;$team++)
{
for($base=1;$base<=$nbase;$base++)
{
$row = mysql_fetch_array($query);
echo "Team $team Base $base = {$row['id']}<br />";
}
for($pivot=1;$pivot<=$npivots;$pivot++)
{
$row = mysql_fetch_array($query);
echo "Team $team Pivot $pivot = {$row['id']}<br />";
}
}
mysql_close();
I thought initially to generate the number of teams but I realized that it might not have a huge number of players. If at first wanted to be able to set/input the number of teams, bases and pivots now I want to enter only the number of bases and pivots to generate teams. Example: I will input 3 bases with 2 pivots and the result should be:
team 1 base 1 pivot 1 pivot 2
team 2 base 1 pivot 1 pivot 2
team 3 base 1 pivot 1 pivot 2
Every selected base and pivots must be unique in order to generate different teams!
来源:https://stackoverflow.com/questions/19788210/select-random-teams-of-players-base-and-pivots