php 学生排名

zbdgwd5y  于 2023-10-15  发布在  PHP
关注(0)|答案(4)|浏览(149)

我试图通过使用总分数来获得学生的排名,也就是说,比其他学生分数高的学生应该是第一个等等,我使用while循环来做到这一点,但是当两个或更多的学生有相同的总分数时,他们得到不同的排名,我想实现的是当两个或更多的学生有相同的总分数时,他们应该有相同的排名,请帮助,这是我的代码。

  1. <?php require_once("include/connection.php"); ?>
  2. <?php
  3. $query = "SELECT * FROM `total` ORDER BY `total` DESC";
  4. $result = mysql_query($query) or die(mysql_error());
  5. $rank = 1; // initialize
  6. echo "<table><tr><th>Student Code</th><th>Rank</th><th>Total</th></tr>\n";
  7. while($row = mysql_fetch_assoc($result))
  8. {
  9. echo "<tr><td>{$row['student_code']}</td><td>$rank</td><td>{$row['total']}</td></tr>\n";
  10. if ($rank == 100)
  11. { break; }
  12. $rank++;
  13. }
  14. echo "</table>\n";
  15. ?>
mrwjdhj3

mrwjdhj31#

保持当前代码,但在循环外添加一个变量,如下图所示,多一个变量以保持当前分数:

  1. $current_rank = 1;
  2. $current_score = null;

在循环中,检查总数是否与您保留的总数相同,如果不同,则将rank分配给当前rank:

  1. if ($current_score != $row['total'])
  2. {
  3. $current_rank = $rank;
  4. }

始终显示$current_rank,只有当它与前一个不同时才会改变,并且在每次迭代结束时,也更新$current_score

  1. $current_score = $row['total'];

我希望这能帮上忙。

展开查看全部
utugiqy6

utugiqy62#

你需要考虑学生的“总体”。
首先,如果你只想要100条记录,你可以在SQL中限制它,这将比循环100次迭代后的break更有效:

  1. $query = "SELECT * FROM `total` ORDER BY `total` DESC LIMIT 100";

想一想,如果你有10,000名学生,你会白白收回9,900名学生。
然后,你的while循环可以这样修改:

  1. $rank = 1;
  2. $lastTotal = 0;
  3. while($row = mysql_fetch_assoc($result))
  4. {
  5. echo "<tr><td>{$row['student_code']}</td><td>$rank</td><td>{$row['total']}</td></tr>\n";
  6. if ($lastTotal == 0) {
  7. $lastTotal = $row['total']; // First time that $lastTotal is set.
  8. }
  9. if ($lastTotal > $row['total']) {
  10. $lastTotal = $row['total'];
  11. $rank++;
  12. }
  13. }

使用此选项,只有当当前学生的排名低于他之前的学生时,您才会增加排名。如果你知道你的maxtotal值,你可以删除第一个检查,如果第一次设置了$lastToal。如果是,例如它是100,只需在$lastTotal = 100中设置它,并删除while循环中的第一个if。

  1. $rank = 1;
  2. $lastTotal = 100;
  3. while($row = mysql_fetch_assoc($result))
  4. {
  5. echo "<tr><td>{$row['student_code']}</td><td>$rank</td><td>{$row['total']}</td></tr>\n";
  6. if ($lastTotal > $row['total']) {
  7. $lastTotal = $row['total'];
  8. $rank++;
  9. }
  10. }
展开查看全部
vnzz0bqm

vnzz0bqm3#

请使用mysqli_*函数mysql_*函数现在很旧了

  1. <?php
  2. require_once("include/connection.php");
  3. $query = "SELECT * FROM `total` ORDER BY `total` DESC";
  4. $result = mysql_query($query) or die(mysql_error());
  5. $rank = $previous = 0;
  6. echo "<table><tr><th>Student Code</th><th>Rank</th><th>Total</th></tr>";
  7. while($row = mysql_fetch_assoc($result))
  8. {
  9. // break statement at the end has no effect since you are doing echo on top.
  10. if ($rank == 100)break;
  11. // If same total will skip $rank++
  12. if($row['total'] != $previous)$rank++;
  13. echo "<tr><td>".$row['student_code']."</td><td>$rank</td><td>".$row['total']."</td></tr>";
  14. // Current row student's total
  15. $previous = $row['total'];
  16. }
  17. echo "</table>";
  18. ?>
展开查看全部
zsohkypk

zsohkypk4#

它非常简单,我用在查询[按标记描述顺序]和它的显示记录排名1到最后排名。很简单.

相关问题