title: Combine function that populates `select` elements with function that pre-selects options matching db values
body: <p>I want to modify function <em>checkSelectedOption</em> so that I don't have to hardcode the array every time it's called.</p>
  
  <p>The 1rst function pre-selects the option matching the information in the db, but it uses hardcoded arrays to populate <code>select</code> elements. The 2nd function creates arrays from db values and dynamically populates <code>select</code> elements. How can I combine these two functions so that I can populate <code>select</code> elements dynamically, and then pre-select the option matching the information in the db? </p>
  
  <p><em>Function checkSelectedOption</em> populates the element with the values specified in array "$options", and then pre-selects the option matching the information in the "customer_info" table of the db.  This function currently requires that the arrays be hardcoded, and I want to replace this part of the function with the method from <em>function printSelectOptions</em> which uses arrays created from db values. </p>
  
  <p><em>Function printSelectOptions</em> populates <code>select</code> elements from values fetched from the db. It does this by creating an array with the values in the column fetched from the "form_fields" table.  </p>
  
  <p><em>Function checkSelectedOption</em></p>
  
  <pre><code>function checkSelectedOption($dataArray, $currentSelection) {
      foreach ($dataArray as $key =&gt; $value) {
          echo '&lt;option ' . (($key == $currentSelection) ? 'selected="selected"' : '') . ' value="' . $key . '"&gt;' . $value . '&lt;/option&gt;';
      }
  }
  try {  
  $stmt = $conn-&gt;prepare("SELECT * FROM customer_info WHERE user_id = :user_id");  
  $stmt-&gt;bindValue(':user_id', $user_id); 
  $stmt-&gt;execute();
  } catch(PDOException $e) {
      echo $e-&gt;getMessage();
  } 
  $row = $stmt-&gt;fetch();
  </code></pre>
  
  <p><em>To call function</em></p>
  
  <pre><code>&lt;select name="first_name"&gt;
      &lt;?php
      // Populates the element with values from array $options         
      $options = array("John"=&gt;"John", "Robert"=&gt;"Robert");
      $selected = $row['first_name'];
      echo checkSelectedOption($options, $selected);
      ?&gt;
  &lt;/select&gt;            
  </code></pre>
  
  <p><em>Function printSelectOptions</em></p>
  
  <pre><code>function printSelectOptions($dataArray, $currentSelection, $fieldname) {
      $output = '';
      foreach ($dataArray as $key =&gt; $value) {
      // Only add to array if row has value
      if ($value[$fieldname] !="" &amp;&amp; $value[$fieldname] !="Null") {
              $output .= '&lt;option ' . (($key == $currentSelection)) . ' value="' . $key . '"&gt;' . str_replace('_', ' ', $value[$fieldname]) . '&lt;/option&gt;';
          }
      }
      return $output;
  }
  try {  
      $stmt = $conn-&gt;prepare("SELECT * FROM form_fields");  
      $stmt-&gt;execute();
  } catch(PDOException $e) {
      echo $e-&gt;getMessage();
  } 
  $rows = $stmt-&gt;fetchAll();
  </code></pre>
  
  <p><em>To call function</em></p>
  
  <pre><code>&lt;select name="first_name"&gt;
      &lt;?php 
      // Dynamically populates element with array created from column first_name
      echo printSelectOptions($rows, $currentSelection, 'first_name');
      ?&gt;
  &lt;/select&gt;  
  </code></pre>