<?php
include("module/stat-menu.php");
  $result = mysql_query("SELECT clan_data.*,char_name,csum,ccount,cavg FROM clan_data INNER JOIN characters ON clan_data.leader_id=characters.obj_Id LEFT JOIN (SELECT clanid, SUM(level) AS csum, count(level) AS ccount, AVG(level) AS cavg FROM characters WHERE clanid GROUP BY clanid) AS levels ON clan_data.clan_id=levels.clanid ORDER BY clan_level DESC, csum DESC", $link)
    or die ("Error: ".mysql_error());

print '<table>
<caption><br><b>..:: Топ кланов ::..</b><br><br><hr></caption>
<thead>
<tr style="vertical-align: bottom;">
<th>Имя клана</th>
<th>Лидер</th>
<th class="sortedColumn">Уровень</th>
<th>Замок</th>
<th>Сумма уровней</th>
<th>Членов</th>
<th>Средний уровень</th>
</tr>
</thead>
<tfoot>
<tr><td colspan="7" style="text-align: right;"><hr>Всего кланов на сервере: '.mysql_num_rows($result).'</td></tr>
</tfoot>
<tbody id="TblBdy">
';

  $i=1;
  while ($row=mysql_fetch_array($result))
  {
    switch ($row["hasCastle"])
    {
      case 1: $row["hasCastle"]="<font color=#008000><b>Gludio</b></font>"; break;
      case 2: $row["hasCastle"]="<font color=#008000><b>Dion</b></font>"; break;
      case 3: $row["hasCastle"]="<font color=#008000><b>Giran</b></font>"; break;
      case 4: $row["hasCastle"]="<font color=#008000><b>Oren</b></font>"; break;
      case 5: $row["hasCastle"]="<font color=#008000><b>Aden</b></font>"; break;
      case 6: $row["hasCastle"]="<font color=#008000><b>Innadril</b></font>"; break;
      case 7: $row["hasCastle"]="<font color=#008000><b>Goddard Castle</b></font>"; break;
      case 8: $row["hasCastle"]="<font color=#008000><b>Rune Castle</b></font>"; break;
      case 9: $row["hasCastle"]="<font color=#008000><b>Schuttgart Castle</b></font>"; break;
      default: $row["hasCastle"]="<font color=#FF0000>Нет</font>";
    }
    print "<tr". (($i++ % 2) ? "" : " class=\"alternateRow\"") ."><td><a href=\"index.php?id=clanview&clan_name=". $row["clan_name"]."\">". htmlspecialchars($row["clan_name"]). "</a></td><td>". $row["char_name"]. "</td><td class=\"numeric sortedColumn\">".$row["clan_level"]. "</td><td>".$row["hasCastle"]. "</td><td class=\"numeric\">".$row["csum"]. "</td><td class=\"numeric\">".$row["ccount"]. "</td><td class=\"numeric\">".$row["cavg"]. "</td></tr>\n";
  }
  print "</tbody>\n</table>\n";
  mysql_close($link);
?>
</body>
</html>
