PHP 递增数,每次创建一个新的div

标签 php mysql html increment ranking

基本上我有一个 PHP 脚本为数据库中的每个项目创建一个 div,但我想在每个创建的 div 中有一个“排名”数字,即:

-----------------
Rank: 1      < div 1
-----------------
Rank: 2      < div 2
-----------------
Rank: 3      < div 3
-----------------

等等..

这是我当前的代码...

while($row = mysql_fetch_array($result)) {

    $name = stripslashes($row['name']);
    $description = stripslashes($row['description']);
    $votes = stripslashes($row['votes']);
    $id = ($row['id']);
    $link = ($row['link']);
    $rank = 0;
    ?>

    <div class="site" id="site">
    <u><center>
    <strong><a href="<?php echo $link ?>" target="_blank"><?php echo $name; ?></a></strong></u>
    </font></center>
    <p>Rank:<?php echo $rank++ ; ?></p>
    <p><b><?php echo $description; ?></b><br />
    Votes:<b> <?php echo $votes; ?></b><br />
    </p>
    </div>

   <center>
    <?php
}
?>

但这不起作用,我们将不胜感激任何帮助。 (此外,div 在多个页面上继续)。

最佳答案

$rank = 0; 放在循环之外。否则它将始终为 0。

关于PHP 递增数,每次创建一个新的div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6170180/

相关文章:

php - 使用引擎 myisam 或 innodb 获取 MySQL 中数据库最近 24 小时更新的表列表

javascript - 在 IE8 中设置单选按钮和复选框的样式

html - 简单的幻灯片仅使用 css 和 html

php - 基于父子关系对数组进行排序的算法

java - MD5 哈希不同

php - 如何使用 codeigniter 插入数据库而不使用单引号?

html - 页脚在 IE11 上出现损坏

php - 加入 - 语法错误

PHP DateTime 异常和错误处理

java - EntityManager.find 方法导致 MySQLIntegrityConstraintViolationException