PHP:一个“排名”系统

时间:2013-03-30 20:27:41

标签: php mysql profile rank

我有一个由用户组成的mysql数据库。在该数据库中,有一个“得分”列。我想为用户制作个人资料,但会显示他们的排名。我不想这样服务器端,所以我试图从配置文件页面本身。这是我目前的代码:

$sql3 = mysql_query("SELECT * FROM members ORDER BY score DESC") or die("Could not allocate information!");
$rank = 0;
while(mysql_fetch_assoc($sql3)){
  $rank++;
}
echo "<b>Rank: </b>#$rank<br/>";

我也尝试过:

$sql3 = mysql_query("SELECT * FROM members ORDER BY score DESC") or die("Could not allocate information!");
$rank = 0;
while(mysql_fetch_assoc($sql3)){
  $rank++;
  echo "<b>Rank: </b>#$rank<br/>";
}

无论哪种方式,每个用户都有相同的等级,#2。我有什么方法可以做到这一点吗?如果您需要更多信息,请发表评论,不要低估我。感谢。

5 个答案:

答案 0 :(得分:0)

你试过了吗?

$sql3 = mysql_query("SELECT * FROM members ORDER BY score DESC") or die("Could not allocate information!");
$rank = 0;
while(mysql_fetch_assoc($sql3)){
    $rank++;
    echo "<b>Rank: </b>#$rank<br/>";
}

答案 1 :(得分:0)

只需使用<ol> [有序列表]标记:

<ol>
<?php while($user = mysql_fetch_assoc($sql3)): ?>

    <li><?php echo $user['name']; ?></li>

<?php endwhile; ?>
</ol>

猜猜你想要显示用户的某些属性[例如name或您在表格中称之为的内容],否则显示数字列表没有多大意义。

无论如何{@ 1}}函数已被弃用,请转到mysql_*PDO

答案 2 :(得分:0)

尝试这样的事情:

$sql3 = mysql_query("SELECT * FROM members ORDER BY score DESC") or die("Could not allocate information!");
$rank = 0;
while($row = mysql_fetch_assoc($sql3)) {
     $rank++;
     if ($row['user_id'] == $current_pages_user_id){
           echo "<b>Rank: </b>#$rank<br/>";
     }
}

答案 3 :(得分:0)

尝试

while($user = mysql_fetch_assoc($sql3)) {    
  $rank++;    
  echo "<b>#$rank. $user['name'] Score: </b> $user['score']<br />";    
}

但正如hjpotter92指出的那样,请研究使用mysqli或pdo。

答案 4 :(得分:0)

我认为你真正想要做的就是这样。

$result = mysql_query("SELECT member_name, score,
       (SELECT COUNT(*)
        FROM members ORDER BY score DESC) AS rank
FROM members
WHERE member_id = 5
LIMIT 1") or die("Could not allocate information!");

$member = mysql_fetch_assoc($result);

echo "<b>#$member['rank']. $member['name']</b> Score:  $member['score']<br />";

如前所述,请查看mysqli和pdo。