排名/阶梯系统

时间:2011-11-20 17:48:24

标签: php mysql sql ranking

我正在尝试创建一个排名/阶梯系统,其中排名最高的部族(1)应该位于顶部,而战队排名第二,依此类推。但我不能让这个工作......我尝试过不同的方法:

PHP:

$sql = mysql_query("SELECT * FROM clans ORDER BY rating DESC");
while($row=mysql_fetch_array($sql))
{
   $totalclans = $row['id']; 
}

$clans = 1;
$clansnumber = $totalclans;

while($clans <= $totalclans)
{
   $sql2 = mysql_query("SELECT * FROM clans WHERE id='$clansnumber'"); 
   while($row=mysql_fetch_array($sql2))
   {
      $name = $row['name'];
   }

   $sql2 = mysql_query("SELECT COUNT(*) AS totalmembers FROM members WHERE clan='$name'"); 
   while($row=mysql_fetch_array($sql2))
   {
      $totalmembers = $row['totalmembers'];
   }

   $sql2 = mysql_query("SELECT * FROM clans WHERE id='$clansnumber'");
   while($row=mysql_fetch_array($sql2))
   {
      echo "<div class='newestmemberusername'><a href='clan.php?clan=" . $row['name'] . "'>" . $row['name'] . ":</a></div>";
      echo "<table class='profileinfo' cellpadding='5' cellspacing='0' border='0'>";
      echo "<tr><td rowspan='4' style='border:none;'><div class='rankbox'>" . $row['rating'] . "</div></td></tr>";
      echo "<tr><td style='" . $firstrowleft . " width: 200px;'>Members:</td><td class='profileinfosecondrow' style='" . $firstrowright . "'>" . $totalmembers . "</td></tr>";
      echo "<tr><td style='" . $color2 . " width: 200px;'>Victories:</td><td class='profileinfosecondrow' style='" . $color2 . "'>" . $row['victories'] . "</td></tr>";
      echo "<tr><td style='" . $lastrowleft1 . " width: 200px;'>Losses:</td><td class='profileinfosecondrow' style='" . $lastrowright1 . "'>" . $row['losses'] . "</td></tr>";
      echo "</table>";

      $clans ++;
      $clansnumber --;
   }
}

echo "</div>";
echo "<div class='contentboxbodybottom'></div>";
}

不幸的是,它只显示了所有的部落,而不是在他们的等级之后命令它们。我已经尝试将id更改为排名并更改它以便首先检查排名1然后排名2然后排名第10(最高排名),但是它仍然只显示所有的部落,我不会想要它。

2 个答案:

答案 0 :(得分:2)

你能解释为什么你使用了很多while语句

试试这个

 $sql = mysql_query("SELECT * FROM clans ORDER BY rating DESC");
                while($row=mysql_fetch_array($sql)) {
                    $totalclans = $row['id'];  
                    $name = $row['name'];

            echo $id.'<br/>'.$name;


  }

如果您遇到任何问题,请告诉我,然后尝试详细说明您实际从数据库中获取的内容

答案 1 :(得分:0)

$sql2 = mysql_query("SELECT * FROM clans WHERE id='$clansnumber' ORDER BY rating DESC LIMIT 0, 10");