PHP表排行榜

时间:2018-10-13 23:11:04

标签: php mysqli html-table

我正在尝试制作自己喜欢的电影的排行榜。

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

<?php
    $con=mysqli_connect("localhost", "", "", "movies");
    $sql = ("SELECT score, year FROM mymovies ORDER BY score DESC");
    $result = mysqli_query($con, $sql);
    $rank = 1;

    if (mysqli_num_rows($result)) {
        while ($row = mysqli_fetch_assoc($result)) {
            echo "<td>{$rank}</td>
                  <td>{$row['score']}</td>
                  <td>{$row['number_of_times']}</td>";
            $rank++;
        }
    }
?>
</table>

问题在于它只显示一行数据,而不是像下面这样相互显示:

Rank Score Year
1    25    1999
2    23    1987
3    20    2005

1 个答案:

答案 0 :(得分:0)

可能是因为您需要包含一个<tr>标记..

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

<?php
    $con=mysqli_connect("localhost", "", "", "movies");
    $sql = ("SELECT score, year FROM mymovies ORDER BY score DESC");
    $result = mysqli_query($con, $sql);
    $rank = 1;

    if (mysqli_num_rows($result)) {
        while ($row = mysqli_fetch_assoc($result)) {
            echo "<tr>
                      <td>{$rank}</td>
                      <td>{$row['score']}</td>
                      <td>{$row['number_of_times']}</td>
                  </tr>";
            $rank++;
        }
    }
?>
</table>