title: How to make a “Sort By” dropdown list with MYSQL data
body: <p>Hey guys I've been stuck on this for a while now and really need help with this. It is to the point where I`m sure it is easy to do but I'm just not seeing it the right way.</p>
  
  <p>What I have is a page called "company_list.php" and on this page I am displaying all companies in the company table. What I want to do is have a drop down selection form to sort my results by. At the moment I am just trying to sort by category.</p>
  
  <p>table company:
  companyID
  categoryID
  companyName
  etc...</p>
  
  <p>table category:
  categoryID
  categoryName</p>
  
  <pre><code>**FORM**
      &lt;form action="company_list.php" method="POST" &gt;
  &lt;label&gt;Category: &lt;/label&gt;
  &lt;select name="categoryID"&gt;
  &lt;?php foreach ($categorys as $category) : ?&gt;
  &lt;option value="&lt;?php echo $category['categoryID']; ?&gt;"&gt;
  &lt;?php echo $category['categoryName']; ?&gt;
  &lt;/option&gt;
  &lt;?php endforeach; ?&gt;
  &lt;/select&gt;
  &lt;input type="submit" value="Sort" /&gt;
  </code></pre>
  
  <p><strong>PHP</strong> This is where I am stuck and have tried many different things PLEASE HELP!!</p>
  
  <pre><code>    //include db here
  //global
  global $db;
  if(isset($_POST['categoryID']) === " '.$categoryID.' ") {
  $query='SELECT *
  FROM company
  WHERE categoryID ="'.$categoryID.'"
  $db-&gt;query($query);
  }
  //get data
  //get all cats
  $query='SELECT * FROM category
  ORDER BY categoryID';
  $categorys=$db-&gt;query($query);
  //Get all COMPANYS for cat
  $query='SELECT * FROM company WHERE categoryID = "'.$categoryID.'"
  ORDER BY companyName ASC';
  $category_companys=$db-&gt;query($query);
  </code></pre>
  
  <p>I`ve hit a wall here and would really love some help. Thanks in advance.</p>
  
  <p>I was able to accomplish something similar to this but not in a drop down list. Below is what I have done but idk how to get it into a drop down.</p>
  
  <p><strong>PHP</strong></p>
  
  <pre><code>    global $db;
  if(!isset($categoryID)) {
  @$categoryID = $_GET['categoryID'];
  if(!isset($categoryID)){
  $categoryID=2; //I want to start by displaying companys for all categorys...not at categoryID=2
  }
  }
  //get name of all categorys
  $query='SELECT * FROM category
  WHERE categoryID = "'.$categoryID.'"';
  $category_results=$db-&gt;query($query);
  $category_row = $category_results-&gt;fetch();
  $categoryName = $category_row['categoryName'];
  //get all cats
  $query='SELECT * FROM category
  ORDER BY categoryID';
  $categorys=$db-&gt;query($query);
  //Get all COMPANYS for cat
  $query='SELECT * FROM company WHERE categoryID = "'.$categoryID.'"
  ORDER BY companyName ASC';
  $category_companys=$db-&gt;query($query);
  </code></pre>
  
  <p><strong>HTML</strong></p>
  
  <pre><code>    &lt;!--lists all category`s in &lt;li&gt; not a drop down form --&gt;
  &lt;?php foreach($categorys as $category) : ?&gt;
  &lt;li&gt;
  &lt;a href="company_list.php?categoryID=&lt;?php echo $category['categoryID']; ?&gt;" &gt;
  &lt;?php echo $category['categoryName']; ?&gt;
  &lt;/a&gt;
  &lt;/li&gt;
  &lt;?php endforeach; ?&gt;
  &lt;!--Result Set--&gt;
  &lt;table border="1px solid black" cellspacing="1px" cellpadding="2px;" style="margin-top:5px;"&gt;
  &lt;!--&lt;h1&gt;Company Name&lt;/h1&gt;--&gt;
  &lt;th&gt;Company Name&lt;/th&gt;
  &lt;th&gt;Address&lt;/th&gt;
  &lt;th&gt;Phone&lt;/th&gt;
  &lt;th&gt;Email&lt;/th&gt;
  &lt;?php foreach($category_companys as $company) : ?&gt;
  &lt;tr&gt;
  &lt;td&gt;&lt;a href="company_page.php?company_id=&lt;?php echo $company['companyID']; ?&gt;"&gt;
  &lt;?php echo $company['companyName']; ?&gt;&lt;/a&gt;
  &lt;/td&gt;
  &lt;td&gt;
  &lt;?php echo $company['companyAddress']; ?&gt;
  &lt;/td&gt;
  &lt;td&gt;
  &lt;?php echo $company['companyPhone']; ?&gt;
  &lt;/td&gt;
  &lt;td&gt;
  &lt;?php echo $company['companyEmail']; ?&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;?php endforeach; ?&gt;
  &lt;/table&gt;
  </code></pre>