简体   繁体   English

从表中提取数据,然后以降序回显

[英]Pull data from table then echo it in descending order

I have the following table in my database click to view 我的数据库中有下表, 点击查看

I'm looking to find everyone whose user_group is "member", then put their ratings in an array and print their names in descending order based on their rating. 我正在寻找user_group是“ member”的每个人,然后将其评级放入数组中,并根据其评级以降序打印其名称。 So in the example given, it would output ted67945<br>ted67942 . 因此,在给出的示例中,它将输出ted67945<br>ted67942

I currently have this code 我目前有这个代码

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>";
}

This, obviously, outputs the numbers. 显然,这将输出数字。 How would I use this to output the names based on their rating? 我将如何使用它来根据名称进行评级输出?

Why can't you just use ORDER BY in your SQL request? 为什么不能仅在SQL请求中使用ORDER BY?

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

This will sort your data by rating in descending order. 这将按评分降序对您的数据进行排序。 See more . 查看更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM