title: Function to check if the db value matches the current Select option
body: <p>I am creating a customer account page which displays the relevant information in the database and allows customers to edit their information. The code below retrieves and displays the customer's account information from the database and pre-selects the values matching their information, but since there are a few hundred select and/or multiselect elements I'd like to create a function which would check if the db value <em>(i.e. <code>$rows['gift_privacy']</code>)</em> matches the current option.</p>
  
  <pre><code>&lt;?php   
  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();
  ?&gt;
  &lt;form action="account_information-exec.php" method="post"&gt;
  &lt;select name="gift_privacy"&gt;
      &lt;option value="Standard" &lt;?php if($row['gift_privacy']=='Standard') echo "selected='selected'"; ?&gt;&gt;Standard&lt;/option&gt;
      &lt;option value="Gift_ID_Req" &lt;?php if($row['gift_privacy']=='Gift_ID_Req') echo "selected='selected'"; ?&gt;&gt;Require program ID&lt;/option&gt;
      &lt;option value="Not_Enrolled" &lt;?php if($row['gift_privacy']=='Not_Enrolled') echo "selected='selected'"; ?&gt;&gt;Do not enroll&lt;/option&gt;
  &lt;/select&gt;
  &lt;input type="submit" value="submit"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>Edit -</strong> The code differs slightly for the multiselect elements. The element names are followed by square brackets <em>(i.e. <code>name="notifications[]"</code>)</em>, and values are inserted into the database as imploded arrays <code>implode(',', $_POST['notifications'])</code>. Those commas are then stripped from fetched values in order to properly compare them to the option's values <code>$row1 = str_replace(',', '', $row);</code></p>
  
  <p><em>Existing Function</em></p>
  
  <p>This function applies <code>selected="selected"</code> to the correct options, but it's too cumbersome for me to use with so many elements and I'm not sure how to modify the code so that it could be easily applied to all the select and/or multiselect elements. Also, since the page currently uses standard select elements with hard-coded options I'm hoping for a solution that doesn't require creating arrays for each element.</p>
  
  <pre><code>&lt;select name="gift_privacy"&gt;
      &lt;?php
      $gifts = array("Standard" =&gt; "Standard", "Gift_ID_Req" =&gt; "Require Program ID", "Not_Enrolled" =&gt; "Do not Enroll");
      $gift = $row['gift_privacy'];
      foreach($gifts as $key =&gt; $value) {
      if($key == $gift) {
          echo '&lt;option selected="selected" value="'. $key .'"&gt;'. $value .'&lt;/option&gt;';
      } else {
          echo '&lt;option value="'. $key .'"&gt;'. $value .'&lt;/option&gt;';
      }
      }
      ?&gt;
  &lt;/select&gt; 
  </code></pre>