title: How to sort items in a HTML table using SQL?
body: <p>I am trying to use <code>$_GET</code> to take a parameter so that if a table heading it selected it will go through my if else statements and sort accordingly. </p>
  
  <p>However, I'm not having much success. I have it set up to sort by ID in ascending order first and I'm just testing by clicking for ID in descending order. Any suggestions would be useful.</p>
  
  <pre><code> if(!isset($_SESSION['userid']))
   {
       header("Location: login.php");
       exit();
   }
  
   try
   {
  
       $sql = 'SELECT * FROM members ORDER by ID';
       $result = $pdo-&gt;query($sql);
  
  
         if($x == 2)
         {
             $sql = 'SELECT * FROM members ORDER by ID DESC';
             $result = $pdo-&gt;query($sql);
         }
  
  
  
  }
  catch (PDOException $e)
  {
       echo 'Error fetching results: ' . $e-&gt;getMessage();
       exit();
  }
  
    while ($row = $result-&gt;fetch())
    {
         $registrations[] = array(
         'ID' =&gt; $row['ID'],
         'email' =&gt; $row['email'],
         'fname' =&gt; $row['fname'],
         'mi' =&gt; $row['mi'],
         'institution' =&gt; $row['institution'],
         'lname' =&gt; $row['lname'],
         'uname' =&gt; $row['uname']);
  
  }
  
  
  
  echo '&lt;table&gt;';
  echo '&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Options&lt;/th&gt;&lt;th&gt;ID&lt;a href="userlist.php?x=1"&gt;
       &amp;utrif;&lt;/a&gt; &lt;a href="userlist.php?x=2"&gt;&amp;dtrif;&lt;/a&gt;&lt;/th&gt;&lt;th&gt;Name 
       &lt;ahref="userlist.php?x=3"&gt;&amp;utrif;&lt;/a&gt;&lt;a href="userlist.php?x=4"&gt; &amp;dtrif;
       &lt;/a&gt;&lt;/th&gt;&lt;th&gt;Institution &lt;a href="userlist.php?x=5"&gt;&amp;utrif;&lt;/a&gt; 
       &lt;a href="userlist.php?x=6"&gt;&amp;dtrif;&lt;/a&gt;&lt;/th&gt;&lt;th&gt;Username 
       &lt;a href="userlist.php?x=7"&gt;&amp;utrif;&lt;/a&gt; 
       &lt;a href="userlist.php?x=8"&gt;&amp;dtrif;&lt;/a&gt;&lt;/th&gt;&lt;th&gt;Email 
       &lt;a href="userlist.php?x=9"&gt;&amp;utrif;&lt;/a&gt; 
       &lt;a href="userlist.php?x=10"&gt;&amp;dtrif;&lt;/a&gt;&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;';
   if($_SESSION['status'] == 1)
   {
       foreach ($registrations as $user)
       {
           echo '&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;a href="userdetails.php?x=' .$user['ID']    
           .'"&gt;VIEW&lt;/a&gt; &lt;/td&gt;
          &lt;td&gt;'.$user['ID'].'&lt;/td&gt;
          &lt;td&gt;'.$user['lname'].", ". $user['fname']." ". $user['mi'].".".'&lt;/td&gt;
          &lt;td&gt;'.$user['institution'].'&lt;/td&gt;
          &lt;td&gt;'.$user['uname'].'&lt;/td&gt;
          &lt;td&gt;'.$user['email'].'&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt;';
  
       }
  
   }
  
   else
   {
  
      echo  '&gt;&lt;tr&gt;&lt;td&gt;&lt;a href="userdetails.php?x='       
     .$_SESSION['userid'].'"&gt;VIEW&lt;/a&gt;&lt;/td&gt; &lt;td&gt;'. $_SESSION['userid'].' &lt;/td&gt;   
     &lt;td&gt;'.$_SESSION['lname'].", ". $_SESSION['fname']." ". 
     $_SESSION['mi'].".".'&lt;/td&gt;&lt;td&gt;'.$_SESSION['institution'].'&lt;/td&gt;
     &lt;td&gt;'.$_SESSION['uname'].'&lt;/td&gt;&lt;td&gt;'.$_SESSION['email'].'&lt;/td&gt;&lt;/tr&gt;';
  
  
  }
  echo '&lt;/table&gt; ';
  
  $x = $_GET['x'];
  
  ?&gt;
  </code></pre>