0

我正在尝试制作结果表。

这是到目前为止该表的样子:

图像

<?php
        $row = mysql_query("SELECT * FROM table ORDER BY Votes DESC LIMIT 5");
        while($sql = mysql_fetch_assoc($row)){
        ?>
        <tr>
            <td width="5%" align="center"><?php $rank=1; echo $rank;?></td>
            <td width="15%" align="center"><img src="/pictures/<?php echo $sql['Picture']; ?>.png" height="50%"></td>
            <td width="7%" align="center"><?php echo $sql['Votes']; ?></td>
        </tr>
              <?php } ?>

这是到目前为止的代码。我正在努力做到这一点,所以排名会自动增加。顺便说一句,排名不是来自数据库,但我只是希望它从 1 开始然后增加。请帮我。

4

2 回答 2

0

您的问题是 $rank 变量是在 while 循环中声明的

<td width="5%" align="center"><?php $rank=1; echo $rank; //this line ?></td>

所以你需要把它移出循环,因为当循环继续时它会再次设置为1(这就是为什么总是显示1)

要解决问题,您应该这样做

<?php
    $row = mysql_query("SELECT * FROM table ORDER BY Votes DESC LIMIT 5");
    $rank = 1; // declare rank here
    while($sql = mysql_fetch_assoc($row)){
    ?>
    <tr>
        <td width="5%" align="center"><?php echo $rank++; // increase rank by 1 each times the loop run ?></td>
        <td width="15%" align="center"><img src="/pictures/<?php echo $sql['Picture']; ?>.png" height="50%"></td>
        <td width="7%" align="center"><?php echo $sql['Votes']; ?></td>
    </tr>
<?php } ?>

希望这有帮助!

于 2013-10-01T02:42:37.370 回答
0
<?php
    $row = mysql_query("SELECT * FROM table ORDER BY Votes DESC LIMIT 5");

    $rank = 0; // default rank
    while($sql = mysql_fetch_assoc($row))
    {
        $rank += 1; // increase
    ?>
        <tr>
            <td width="5%" align="center"><?php echo $rank;?></td>
            <td width="15%" align="center"><img src="/pictures/<?php echo $sql['Picture']; ?>.png" height="50%"></td>
            <td width="7%" align="center"><?php echo $sql['Votes']; ?></td>
        </tr>
<?php } ?>
于 2013-10-01T02:05:09.670 回答