2013-12-09 29 views
0

我有这段代码,它给出了数据库的输出,现在我想订购它相对于TeamPoints。得分较高的球队获得第一名。我如何实现它。排名更新问题

我一直在尝试在数据库中添加一个字段,它通过增加或减少来更新团队排名。但它不起作用,所以我决定按点排列它们,但现在我面临的问题是按顺序排列它们。

<?php 
    $con=mysqli_connect("", "", ", ""); 

    // Check connection: 
    if (mysqli_connect_errno()) 
    { 
     echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 

    mysqli_query($con,"CREATE VIEW Rankings AS 
     SELECT TeamID, TeamName, TeamLeader, TeamEmail, P_1, P_2, P_3, P_4, P_5, S_1, S_2, TeamWins, TeamLoss, TeamPoints 
     FROM team 
     WHERE TeamID>0"); 

    $result = mysqli_query($con,"SELECT * FROM team"); 

    echo "<table border='1'> 
    <tr> 
     <th>TeamID</th> 
     <th>TeamName</th> 
     <th>TeamLeader</th> 
     <th>TeamEmail</th> 
     <th>Player #1</th> 
     <th>Player #2</th> 
     <th>Player #3</th> 
     <th>Player #4</th> 
     <th>Player #5</th> 
     <th>Subsitute #1</th> 
     <th>Subsitute #2</th> 
     <th>Total Wins</th> 
     <th>Total Losses</th> 
     <th>Total Points</th> 
    </tr>"; 

    while($row = mysqli_fetch_array($result)) 
    { 
     echo "<tr>"; 
      echo "<td>" . $row['TeamID'] . "</td>"; 
      echo "<td>" . $row['TeamName'] . "</td>"; 
      echo "<td>" . $row['TeamLeader'] . "</td>"; 
      echo "<td>" . $row['TeamEmail'] . "</td>"; 
      echo "<td>" . $row['P_1'] . "</td>"; 
      echo "<td>" . $row['P_2'] . "</td>"; 
      echo "<td>" . $row['P_3'] . "</td>"; 
      echo "<td>" . $row['P_4'] . "</td>"; 
      echo "<td>" . $row['P_5'] . "</td>"; 
      echo "<td>" . $row['S_1'] . "</td>"; 
      echo "<td>" . $row['S_2'] . "</td>"; 
      echo "<td>" . $row['TeamWins'] . "</td>"; 
      echo "<td>" . $row['TeamLoss'] . "</td>"; 
      echo "<td>" . $row['TeamPoints'] . "</td>"; 
     echo "</tr>"; 
    } 

    echo "</table>"; 
    mysqli_close($con); 
?> 
+1

1.规范化数据 – Strawberry

+1

啊我刚刚做了,现在我有1个数据库与团队信息作为TeamID主键,其他与TeamRank作为主键的团队排名信息 –

回答

0

添加排名来选择并输出,与输出限制到一个页面沿(10 - 轻易改变): -

<?php 
    $con=mysqli_connect("", "", "", ""); 

    // Check connection: 
    if (mysqli_connect_errno()) 
    { 
     echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 

    $PageStart = 10 * intval($_POST['page']); 

    $result = mysqli_query($con,"SELECT TeamID, 
           TeamName, 
           TeamLeader, 
           TeamEmail, 
           P_1, 
           P_2, 
           P_3, 
           P_4, 
           P_5, 
           S_1, 
           S_2, 
           TeamWins, 
           TeamLoss, 
           TeamPoints, 
           TeamRnk 
           FROM 
           (
            SELECT TeamID, 
              TeamName, 
              TeamLeader, 
              TeamEmail, 
              P_1, 
              P_2, 
              P_3, 
              P_4, 
              P_5, 
              S_1, 
              S_2, 
              TeamWins, 
              TeamLoss, 
              TeamPoints, 
              @Rank := @Rank + 1 AS TeamRnk 
            FROM SomeTable 
            CROSS JOIN (SELECT @Rank:=0) Sub0 
            ORDER BY TeamPoints DESC 
           ) Sub1 
           LIMIT $PageStart, 10"); 

    echo "<table border='1'> 
    <tr> 
     <th>TeamRnk</th> 
     <th>TeamID</th> 
     <th>TeamName</th> 
     <th>TeamLeader</th> 
     <th>TeamEmail</th> 
     <th>Player #1</th> 
     <th>Player #2</th> 
     <th>Player #3</th> 
     <th>Player #4</th> 
     <th>Player #5</th> 
     <th>Subsitute #1</th> 
     <th>Subsitute #2</th> 
     <th>Total Wins</th> 
     <th>Total Losses</th> 
     <th>Total Points</th> 
    </tr>"; 

    while($row = mysqli_fetch_assoc($result)) 
    { 
     echo "<tr>"; 
      echo "<td>" . $row['TeamRnk'] . "</td>"; 
      echo "<td>" . $row['TeamID'] . "</td>"; 
      echo "<td>" . $row['TeamName'] . "</td>"; 
      echo "<td>" . $row['TeamLeader'] . "</td>"; 
      echo "<td>" . $row['TeamEmail'] . "</td>"; 
      echo "<td>" . $row['P_1'] . "</td>"; 
      echo "<td>" . $row['P_2'] . "</td>"; 
      echo "<td>" . $row['P_3'] . "</td>"; 
      echo "<td>" . $row['P_4'] . "</td>"; 
      echo "<td>" . $row['P_5'] . "</td>"; 
      echo "<td>" . $row['S_1'] . "</td>"; 
      echo "<td>" . $row['S_2'] . "</td>"; 
      echo "<td>" . $row['TeamWins'] . "</td>"; 
      echo "<td>" . $row['TeamLoss'] . "</td>"; 
      echo "<td>" . $row['TeamPoints'] . "</td>"; 
     echo "</tr>"; 
    } 

    echo "</table>"; 
    mysqli_close($con); 
?> 
+0

我认为有systax的问题​​,即时通讯得到这个错误 警告:mysqli_fetch_assoc()期望参数1是mysqli_result,布尔在给定的C:\ xampp \ htdocs \ teamrankingdb \ rank.php在线69 –

+0

小错字(忘了给子查询一个别名)。立即试用 – Kickstart

+0

谢谢老兄,问题解决! btw我想创建另一个带有teamrank信息的表格如何修改这个以添加TeamWins TeamLoss TeamRank和TeamPoints的其他表格 –

1

这是你要找的吗?

SELECT TeamID,TeamName,TeamLeader,TeamEmail,P_1,P_2,P_3,P_4,P_5,S_1,S_2,TeamWins,TeamLoss,TeamPoints 
FROM team 
where TeamID>0 
ORDER BY TeamPoints DESC; 
+0

是的我试图实现这一点,但我有问题得到输出在PHP表中。 –

0
CREATE VIEW Rankings AS 
    SELECT TeamID 
     , TeamName 
     , TeamLeader 
     , TeamEmail 
     , P_1  -- none 
     , P_2  -- of 
     , P_3  -- these 
     , P_4  -- belong 
     , P_5  -- in a 
     , S_1  -- teams 
     , S_2  -- table 
     , TeamWins -- and even 
     , TeamLoss -- these are 
     , TeamPoints -- questionable 
     FROM team 
    where TeamID > 0; 
+0

P_1到P_5是玩家,其中S_1和S_2是a重新替代的 –

+0

我知道。见正常化。 – Strawberry