title: PHP - How to order data by ascending or descending
body: <p>I have a form that allows a user to retrieve information from a database. </p>
  
  <p>Currently users are able to retrieve the information from the database with the code I have provided (probably terrible coding but it works).</p>
  
  <p>Now I would like the data to display in an asc or dsc order depending on what the user selects on the form. But im not too sure how to go about doing that, any help in the right direction would be much appreciated!</p>
  
  <p>The PHP that retrieves the information:</p>
  
  <pre><code>$sql = "SELECT RunnerID, EventID, Date, FinishTime, Position, CategoryID, AgeGrade, PB FROM Results";
  $result = $conn-&gt;query($sql);
  
  if ($result-&gt;num_rows &gt; 0) {
   echo "&lt;table&gt;&lt;tr&gt;&lt;th&gt;RunnerID&lt;/th&gt;&lt;th&gt;EventID&lt;/th&gt;&lt;th&gt;Date&lt;/th&gt;&lt;th&gt;FinishTime&lt;/th&gt;&lt;th&gt;Position&lt;/th&gt;&lt;th&gt;CategoryID&lt;/th&gt;&lt;th&gt;AgeGrade&lt;/th&gt;&lt;th&gt;PB&lt;/th&gt;&lt;/tr&gt;";
  
   while($row = $result-&gt;fetch_assoc()) {
       echo "&lt;tr&gt;&lt;td&gt;" . $row["RunnerID"]. "&lt;/td&gt;&lt;td&gt;" . $row["EventID"]. " &lt;/td&gt;&lt;td&gt;" . $row["Date"]. " &lt;/td&gt;&lt;td&gt;" . $row["FinishTime"]. " &lt;/td&gt;&lt;td&gt;" . $row["Position"]. " &lt;/td&gt;&lt;td&gt;" . $row["CategoryID"]. " &lt;/td&gt;&lt;td&gt;" . $row["AgeGrade"]. " &lt;/td&gt;&lt;td&gt;" . $row["PB"]. " &lt;/td&gt;&lt;/tr&gt;";
   }
   echo "&lt;/table&gt;";
  } 
  else {
   echo "Error";
  }
  </code></pre>