HTML表格中PHP的自动增加
我想做一个结果表 以下是到目前为止该表的外观图像:HTML表格中PHP的自动增加,php,html,html-table,Php,Html,Html Table,我想做一个结果表 以下是到目前为止该表的外观图像: <?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
<?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 } ?>
.png“height=”50%“>
这是目前为止的代码。我正在尝试使排名自动增加。顺便说一下,排名不是来自数据库,但我只希望它从1开始,然后增加。请帮助我。
<?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 } ?>
.png“height=”50%“>
您的问题是$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 } ?>
.png“height=”50%“>
希望能帮上忙!非常感谢。我没想到会这么简单。