Select random teams of players (base and pivots)

放肆的年华 提交于 2019-12-11 09:14:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!