PHP,排序从MySQL数组 [英] php, sort an array from mysql

查看:102
本文介绍了PHP,排序从MySQL数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跑在三列的查询;一列包含文本,其他两个包含数字。我做的这些数字计算来获得一个名为$平均新号码。然后我吐出结果的HTML表格。在表中的行他们出来的数据库的顺序进行排序。我想排序表,以便将数据从最高$显示平均到最低(同时仍然与第一列正确的文本值正确关联)。

我已经尝试了一些 ASORT 的foreach 的东西,但我只成功地使一个烂摊子错误。

任何想法,我怎么去呢?
谢谢你。

这是玩的当前状态:

  / DB查询
  如果(!$结果= mysqli_query($连接,选择quiz_name,
                                            quiz_attempts,
                                            cumulative_score
                                       FROM scoredata)){
  回声(有一个问题:mysqli_error($链接)。);
  出口();
  }
...
//得到的结果吗?
  如果(mysqli_num_rows($结果)GT = 1){  $输出=;
  $输出=<表> \\ N。
  $输出=< TR><第i测验名和LT; /第i个百分位>玩过< /第i个百分位>平均得分< /第i< / TR> \\ N的;  而($行= mysqli_fetch_array($结果)){    $输出=< TR>< TD>中。str_replace函数('_','',$行['quiz_name'])。 &所述; / TD>中;
    $输出=< TD>中。 。 $行['quiz_attempts']。 &所述; / TD>中;    //计算平均分
    $平均= $行['cumulative_score'] / $行['quiz_attempts'];    $输出=< TD>中。 。轮($平均,2)。 &所述; / TD>&下; / TR>中;
   }  $输出=< /表> \\ N。
  回声$输出;
}
...


解决方案

您可以做计算和查询排序:

  SELECT
    quiz_name,
    quiz_attempts,
    cumulative_score,
    (cumulative_score / quiz_attempts)作为score_avg
从scoredata
ORDER BY score_avg DESC

I'm running a query on three columns; one column contains text, the other two contain numbers. I do a calculation on these numbers to get a new number called $average. I then spit out the result to an html table. The rows in the table are sorted in the order they come out of the database. I'm trying to sort the table so that the data is displayed from highest $average to lowest (while still be correctly associated with the correct text value from the first column).

I've tried some asort and foreach stuff, but I've only succeeded in making a mess of errors.

Any ideas as how I go about this? Thanks.

This is the current state of play:

/ db query
  if (!$result = mysqli_query($link,"SELECT quiz_name, 
                                            quiz_attempts, 
                                            cumulative_score 
                                       FROM scoredata")) {
  echo("There was a problem: " . mysqli_error($link));
  exit();
  }
...
// got results?
  if(mysqli_num_rows($result) >= 1) {

  $output = "";
  $output .= "<table>\n";
  $output .= "<tr><th>Quiz name</th> <th>Played</th> <th>Avg. score</th></tr>\n";

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

    $output .= "<tr><td>".str_replace('_', ' ', $row['quiz_name']) . "</td>";
    $output .= "<td>" . $row['quiz_attempts'] . "</td>";

    // calculate average score
    $average = $row['cumulative_score']/$row['quiz_attempts'];

    $output .= "<td>" . round($average,2) . "</td></tr>";
   }

  $output .= "</table>\n";
  echo $output;
}
...

解决方案

You can do calculation and sorting in your query:

SELECT 
    quiz_name, 
    quiz_attempts, 
    cumulative_score,
    (cumulative_score/quiz_attempts) as score_avg
FROM scoredata
ORDER BY score_avg DESC

这篇关于PHP,排序从MySQL数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆