"排序结果"在查询中的PHP公式

时间:2015-01-17 01:21:31

标签: php sorting

我有以下查询,我想对数学公式“echo”的结果进行排序。(( - $ row [“startweight”] + $ row [“currweight”])/ $ row [“startweight “])。”“;”这创造了一个百分比答案。由于结果中有很多人,我希望能够按从高到低的降序对公式的结果进行排序。我花了相当多的时间研究这个,我感觉它需要在某种阵列中。我希望不是。以下是用于上下文目的的整个查询:

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

感谢您的帮助。 (是的......我是编程的FNG)

2 个答案:

答案 0 :(得分:1)

你可以在SQL中排序:

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

考虑如果startweight是0,你会得到什么

答案 1 :(得分:0)

将您的查询重写为:

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