title: Set sort order of MySQL query using dropdown options
body: <p>I want to allow site users to change the sort order of results that are returned by an existing query.
  The query is currently</p>
  
  <pre><code>$searchList = 'select distinct pa.products_id, pd.products_name,
                 p.products_model
                 FROM ' . TABLE_PRODUCTS_ATTRIBUTES . ' pa
                 left join ' . TABLE_PRODUCTS_DESCRIPTION . ' pd on (pa.products_id = pd.products_id)
                 left join ' . TABLE_PRODUCTS . ' p on (pa.products_id = p.products_id)
                 WHERE pd.language_id = ' . $language_id . '
                 order by products_model';
  </code></pre>
  
  <p>I've added the following code to generate the dropdown</p>
  
  <pre><code>&lt;select id = "SortBy"&gt;
  &lt;option value =""&gt;Please Choose&lt;br&gt;    
  &lt;option value ="products_model;"&gt;Model (asc)&lt;br&gt;
  &lt;option value ="products_model DESC;"&gt;Model (desc)&lt;br&gt;
  &lt;option value ="products_name;"&gt;Name (asc)&lt;br&gt;
  &lt;option value ="products_name DESC;"&gt;Name (desc)&lt;br&gt;
  &lt;option value ="products_id;"&gt;Product ID (asc)&lt;br&gt;
  &lt;option value ="products_id DESC;"&gt;Product ID (desc)
  &lt;/select&gt;
  </code></pre>
  
  <p>and updated the query to</p>
  
  <pre><code>$searchList = 'select distinct pa.products_id, pd.products_name,
                 p.products_model
                 FROM ' . TABLE_PRODUCTS_ATTRIBUTES . ' pa
                 left join ' . TABLE_PRODUCTS_DESCRIPTION . ' pd on (pa.products_id = pd.products_id)
                 left join ' . TABLE_PRODUCTS . ' p on (pa.products_id = p.products_id)
                 WHERE pd.language_id = ' . $language_id . '
                 order by ".mysql_real_escape_string($sort)."';
  </code></pre>
  
  <p>After the select options and before the sql query I've added</p>
  
  <pre><code>$sort=$_POST["SortBy"]
  </code></pre>
  
  <p>which i believe should contain the selection made once it's submitted.</p>
  
  <p>This is where I'm a little stuck. I want the user to be able to select their option from the dropdown and have it auto submit and store the choice so that the query which follows uses their selection but I don't know how to do this. I'm assuming some type of jQuery, but to be honest I'm lost when it comes to coding in this.</p>
  
  <p>Following comments from Jeff I have changed the select list to this</p>
  
  <pre><code>&lt;select id = "SortBy" name="SortBy&gt;
  &lt;option value =""&gt;Please Choose&lt;br&gt;    
  &lt;option value ="model+"&gt;Model (asc)&lt;br&gt;
  &lt;option value ="model-"&gt;Model (desc)&lt;br&gt;
  &lt;option value ="name+"&gt;Name (asc)&lt;br&gt;
  &lt;option value ="name-"&gt;Name (desc)&lt;br&gt;
  &lt;option value ="prodid+"&gt;Product ID (asc)&lt;br&gt;
  &lt;option value ="prodid-"&gt;Product ID (desc)
  &lt;/select&gt;
  </code></pre>
  
  <p>and added the following code</p>
  
  <pre><code>    switch ($_SESSION ['sba_sort_order']) {
      case 'model+' :
          $order_by = ' products_model; ';
          break;
      case 'model-' :
          $order_by = ' products_model DESC; ';
          break;
      case 'name+' :
          $order_by = ' products_name; ';
          break;
      case 'name-' :
          $order_by = ' products_name DESC; ';
          break;
      case 'prodid+' :
          $order_by = ' products_id; ';
          break;
      case 'prodid-' :
          $order_by = ' products_id DESC; ';
          break;
  </code></pre>
  
  <p>Still lost on how to get this to trigger automatically and store the result for the sql query on selection of a dropdown though.</p>