我的数据库中有下表
click to view

我正在寻找user_group是“成员”的每个人,然后将其评分放入数组中,并根据其评分以降序打印其名称。
因此,在给出的示例中,它将输出ted67945<br>ted67942

我目前有此代码

function getInfo($info, $given_username) { // This is only used to get the rating of the given user
  require($_SERVER["DOCUMENT_ROOT"]."/movies/scripts/db.php");
  $result = mysqli_query($conn, "SELECT ".$info." FROM members WHERE username='$given_username'");
  while ($row = mysqli_fetch_array($result)) { // for each member
    return $row[$info];
  }
}
$result = mysqli_query($conn, "SELECT username FROM members WHERE user_group='member'") or die(mysql_error());
$ratings = Array();
while ($row = mysqli_fetch_array($result)) { // for each member
  $name = $row['username'];
  if(!empty($name)) {
    array_push($ratings, getInfo("rating", $name)); // This puts all people's ratings in group "member", into an array
  }
}
rsort($ratings);
foreach($ratings as $x) {
  echo $x . "<br>";
}


显然,这将输出数字。我将如何根据名称对它们进行评级?

最佳答案

为什么不能仅在SQL请求中使用ORDER BY?

SELECT username FROM members WHERE user_group='member' ORDER BY rating DESC;


这将按评分降序对您的数据进行排序。 See more

关于php - 从表中提取数据,然后以降序回显,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49100800/

10-12 07:05