douyou2234 2013-12-09 17:38
浏览 51
已采纳

排名更新问题

I have this code, its gives the output of the Database, now I want to order it with respect to TeamPoints. The team with higher points gets the first position. How do I Implement it.

I have been trying to add a TeamRank field in the Database, which updates a team Rank by increment or decrement. But it's not working so I decided to just order them by points, but now I am facing issues in arranging them in increasing order.

<?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);
?>
  • 写回答

3条回答 默认 最新

  • douluo3256 2013-12-10 09:31
    关注

    Adding a rank to the select and outputting it, along with limiting the output to a page (of 10 - easily changed):-

    <?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);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试