-1

桌上玩家

播放器1

播放器2

播放器3

播放器4

播放器5

...

播放器100

ETC

我需要选择 10 个具有 4 名球员的独特随机球队作为:1 个基地和 3 个支点

目前正在尝试使用:

    require_once "connect_to_mysql.php"; 
    $sqlCommand = "SELECT id FROM players ORDER BY RAND() LIMIT 10"; 
    $query = mysql_query($sqlCommand) or die (mysql_error()); 
    $i =1;
    while ($row = mysql_fetch_array($query)) { 
        echo "$i Base  = ";
        echo $row['id'];
        echo "<br />";  
        echo 'Pivot';   
        echo "<br />";
        if ($i % 4 == 0)
     {
       echo '<br />';
     }
     $i++;
    } 
    mysql_close();

结果必须是:

**team 1**
Base 1
Pivot 1
Pivot 2
Pivot 3

**team 2**
Base 1
Pivot 1
Pivot 2
Pivot 3

**team 3**
Base 1
Pivot 1
Pivot 2
Pivot 3

**team 4**
Base 1
Pivot 1
Pivot 2
Pivot 3

**team 5**
Base 1
Pivot 1
Pivot 2
Pivot 3

**team 6**
Base 1
Pivot 1
Pivot 2
Pivot 3
4

2 回答 2

0

这个简单的代码应该可以工作:

$i = 0;
while ($row = mysql_fetch_array($query)) {
    if($i % 4 == 0) 
        echo "<BR /> Base  = " . $row['id'];
    else 
        echo " Pivot : ". $row['id'];
    $i++;
}

格雷格

于 2013-11-05T10:11:37.780 回答
0

如果为此执行多个查询,团队可能会重叠。这一切都需要在一个查询中完成,该查询随机选择 40 名球员,然后将他们分配给球队:

$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();

编辑:根据您的评论更改代码以参数化团队、基础球员和枢轴的数量。

于 2013-11-05T10:13:57.383 回答