0

我整天都在为这个问题撞墙,我知道该怎么做。我希望这不是很简单,因为这对我来说是一场斗争!我已经搜索了一整天,希望有人可以帮助我。

我有一个 html 表格,显示训练营中每个球员的体能测试结果。我已经弄清楚如何将它们连同它们的名称一起显示(取自另一个 mysql 表)。现在,在每个测试结果旁边,我想添加它们对特定测试结果的排名。这是现在减去过滤部分的代码(我认为不需要它来解决这个问题):

数据库查询如下。它按位置对桌上的球员进行排序,以便所有防守队员一起出现,所有前锋一起出现,守门员一起出现,以便教练轻松查看。

//get the camp id for the coach logged in
$getcampid = "SELECT value FROM profile_values WHERE uid = '$user->uid'";   
$campid = mysql_query ($getcampid);
$campidrow = mysql_fetch_array($campid);
$coachescamp = $campidrow[0];

$getplayerdata = mysql_query("SELECT * FROM content_type_test 

INNER JOIN content_type_player ON content_type_test.field_player_uid=
content_type_player.field_uid 

WHERE content_type_test.field_training_camp_nid='$coachescamp' 

ORDER BY content_type_player.field_position_value") or die(mysql_error());

$left = true;
while($playerdata=mysql_fetch_array($getplayerdata))
{

echo "<td>", $playerdata['field_position_value'], "</td>";
echo "<td>", $playerdata['field_last_name_value'],", ",$playerdata['field_first_name_value'], "</td>";
echo "<td>", $playerdata['field_speed_value'], "</td>";
echo "<td>Show rank of players speed compared to other players in $getplayerdata query</td>";
echo "<td>", $playerdata['field_agility_value'], "</td>";
echo "<td>Show rank of players agility compared to other players in $getplayerdata query</td>";
echo"</tr>";
}
echo"</tbody></table>";

我需要在“显示玩家等级......”的列中显示玩家速度和敏捷度的等级。总共还有 5 个等级,我需要在该表中显示,我没有包括在内,以保持简单。如果我能看到如何为这两个做这件事,我就可以为其他人做这件事。

注意:速度和敏捷是数字。数组中的最低数字应排名第一。

感谢您提供的任何帮助,这些帮助一直在杀死我。

编辑 - - - - - - - - - - - - - - - - - - -

这是我只返回 1 个结果的代码

$getplayerdata = mysql_query("SELECT * FROM content_type_test 

INNER JOIN content_type_player ON content_type_test.field_player_uid=
content_type_player.field_uid 

WHERE content_type_test.field_training_camp_nid='$coachescamp' 

ORDER BY content_type_player.field_position_value") or die(mysql_error());

$left = true;
while($playerdata=mysql_fetch_array($getplayerdata))
{

$arrayOfAllData = array();
while($playerdata=mysql_fetch_array($getplayerdata)){
$arrayOfAllData[$playerdata[ID]] = array(field_position_value =>$playerdata['field_position_value'],
                                      field_last_name_value=>$playerdata['field_last_name_value'], 
                                      field_speed_value=>$playerdata['field_speed_value']);

}

$sql = mysql_query("SELECT @rownum:=@rownum+1 'rank', * FROM content_type_test, (SELECT @rownum:=0) r 
INNER JOIN content_type_player ON content_type_test.field_player_uid=
content_type_player.field_uid 
WHERE content_type_test.field_training_camp_nid='$coachescamp' 
ORDER BY content_type_player.field_speed_value");

while($playerdata=mysql_fetch_array($sql)){
    $arrayOfAllData[$playerdata[ID]]["field_speed_value"] = $playerdata['field_speed_value'];
}

foreach($arrayOfAllData as $player){
   echo "<td>", $player['field_position_value'], "</td>";
   echo "<td>", $player['field_last_name_value'],", ",$player['field_first_name_value'], "</td>";
   echo "<td>", $player['field_speed_value'], "</td>";
   echo "<td>*I dont know what to put here to display the rank*</td>";   
   echo"</tr>";
}
}
echo"</tbody></table>";

打印显示一行这样的数组([] => 数组([field_position_value] => Forward [field_last_name_value] => Tester [field_speed_value] => 4.504))

同样如一栏中所述,我不知道该键入什么来显示排名。

4

2 回答 2

0

好的,因为没有优雅的方法可以使用 1 个 MySQL 查询来执行此操作,所以我会使用多个查询,将它们组合成一个数组并循环遍历它以获得您的结果。这有点骇人听闻,但可以完成工作。

使用您的原始查询,然后不是循环并立即显示,而是循环并放入数组:

$arrayOfAllData = array();
while($playerdata=mysql_fetch_array($getplayerdata)){
$arrayOfAllData[$playerdata[ID]] = array(field_position_value =>$playerdata['field_position_value'],
                                          field_last_name_value=>$playerdata['field_last_name_value'], 
                                          field_speed_value=>$playerdata['field_speed_value'])

}

然后获取您的下一个 MySQL 查询,该查询将按 field_speed_value 对玩家进行排名

$sql = mysql_query("SELECT @rownum:=@rownum+1 ‘rank’, * FROM content_type_test, (SELECT @rownum:=0) r 
INNER JOIN content_type_player ON content_type_test.field_player_uid=
content_type_player.field_uid 
WHERE content_type_test.field_training_camp_nid='$coachescamp' 
ORDER BY content_type_player.field_speed_value");

此查询将为每个玩家添加一个排名,现在您必须将此排名作为一个值添加到 $arrayOfAllData

while($playerdata=mysql_fetch_array($sql)){
    $arrayOfAllData[$playerdata[ID]]["field_speed_value"] = $playerdata['field_speed_value']);
}

为每个排名创建一个新查询,并以相同的方式将其添加到您的数组中。然后当你完成添加到数组中时,你可以像这样输出:

foreach($arrayOfAllData as $player){
   echo "<td>", $player['field_position_value'], "</td>";
   echo "<td>", $player['field_last_name_value'],", ",$player['field_first_name_value'], "</td>";
....etc...
}

希望这会有所帮助,我累了,要睡觉了,如果您有任何问题,明天可以澄清。

于 2012-06-01T08:01:25.403 回答
-1

编辑:抱歉-此查询仅适用于 SQL Server,不适用于 MySQL。

您可以将列添加到将具有排名的 sql 查询:

SELECT *, ROW_NUMBER() OVER (ORDER BY field_speed_value) AS field_speed_Ranking,
ROW_NUMBER() OVER (ORDER BY field_agility_value) AS field_agility_Ranking
FROM content_type_test
INNER JOIN content_type_player ON content_type_test.field_player_uid=   
content_type_player.field_uid 
WHERE content_type_test.field_training_camp_nid='$coachescamp' 
ORDER BY content_type_player.field_position_value") or die(mysql_error());

这将在查询结果中为您提供两个新列,field_speed_Ranking 和 field_agility_Ranking,每个玩家按顺序排列。只需以与其他列相同的方式回显它们即可。

于 2012-06-01T06:32:25.110 回答