简体   繁体   English

查询中php公式的“排序结果”

[英]“sort result” of a php formula in a query

I have the following query and I would like to sort the results of the math formula "echo "" . ((-$row["startweight"] + $row["currweight"])/$row["startweight"]) . " ";" 我有以下查询,我想对数学公式“ echo”“的结果进行排序。(((-$ row [” startweight“] + $ row [” currweight“])/ $ row [” startweight“]) 。“”; which creates a percentage answer. 这将创建一个百分比答案。 As there are many people in the result, I'd like to be able to sort the result of the formula in descending order highest to lowest. 由于结果中有很多人,因此我希望能够按照从高到低的降序对公式的结果进行排序。 I've spent a considerable amount of time researching this and I get the feeling it needs to be in some sort of array. 我花了很多时间研究这个问题,我感到它需要以某种形式排列。 I hope not. 我希望不是。 Here is the entire query for context purposes: 这是出于上下文目的的整个查询:

$sql = "SELECT * FROM members";
$result = $conn->query($sql);
echo "<table border=1>
   <tr>
       <th>ID</th>
       <th>Name</th>
       <th>Start Weight</th>
       <th>Current Weight</th>
       <th>Weight Diff</th>
       <th>Percent Diff</th>
       <th>Goal Weight</th>
       <th>Lbs2Go</th>
   <tr>";


if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
    echo "<tr>";
        echo "<td>" . $row["id"] . " </td>";
        echo "<td>" . $row["name"] . " </td>";
        echo "<td>" . $row["startweight"] . " </td>";
        echo "<td>" . $row["currweight"] . " </td>";
        echo "<td>" . ($row["startweight"] - $row["currweight"]) . " </td>";
        echo "<td>" . ((-$row["startweight"] + $row["currweight"])/$row["startweight"]) . " </td>";
        echo "<td>" . $row["goalweight"] . " </td>";
        echo "<td>" . ($row["currweight"] - $row["goalweight"]) . " </td>";
    echo "</tr>";
     }
    echo "</table>";
  } else {
  echo "0 results";
}

Thanks for your help. 谢谢你的帮助。 (and yes... I'm an FNG to programming) (是的...我是FNG编程人员)

you can sort in SQL: 您可以在SQL中排序:

$sql = "SELECT * FROM members order by (currweight-startweight)/startweight desc";

consider what you will get if startweight is 0 考虑如果起始权重为0,您将获得什么

将查询重写为:

select * FROM members order by (currweight-startweight)/startweight

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

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