phpmysqlhtmlleaderboard

Creating a leaderboards, how would I go about displaying rank/position?


I am creating a leaderboards which will display the following: Rank, Username, Score

I currently have the table to it will display Username and Score from the data in a mysql table, I am just wondering how would I go about displaying a rank for each user, number 1 being the user with the highest score then descending.

Thanks!


Solution

  • I recommend reading up on PHP/MySQL.

    HTML Header: Open your table, create your headers

    <table>
        <tr>
            <td>Rank</td>
            <td>User</td>
            <td>Score</td>
        </tr>
    

    PHP: Dynamically generate the rows for each user

        <?php
    
            $result = mysql_query("SELECT user, score FROM leaderboard ORDER BY score DESC");
            $rank = 1;
    
            if (mysql_num_rows($result)) {
                while ($row = mysql_fetch_assoc($result)) {
                    echo "<td>{$rank}</td>
                          <td>{$row['user']}</td>
                          <td>{$row['score']}</td>";
    
                    $rank++;
                }
            }
        ?>
    

    HTML Footer: need to close the table

    </table>