PLAYER1
player2
player3
player4
player5
...
玩家100
等
我需要选择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
答案 0 :(得分:0)
这个简单的代码应该有效:
$i = 0;
while ($row = mysql_fetch_array($query)) {
if($i % 4 == 0)
echo "<BR /> Base = " . $row['id'];
else
echo " Pivot : ". $row['id'];
$i++;
}
格雷格
答案 1 :(得分: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();
编辑:更改了代码,根据您的评论参数化了团队,基本参与者和支点的数量。