title: PHP - pre select/save option on dynamically populated select dropdowns
body: <p>I have a dropdown box that holds the days 1-31 and i want to store/save what the user has previously selected if they return to the page.</p>
  
  <p>My function to generate the box is:</p>
  
  <pre><code>public function fetchDDMMYYYYDropdown($select_d,$session_d) {
          $days = range (1, 31);
          $dropdown .=  '&lt;select name="'.$select_d.'"&gt;';
          foreach($days as $key=&gt;$name){
              if($session_d==$name){
                  $session = 'selected';
              }
              $dropdown .= '&lt;option value="'.sprintf("%02d", $name).'" selected="'.$session.'"&gt;'.sprintf("%02d", $name).'&lt;/option&gt;';
          }
          $dropdown .=  '&lt;/select&gt;';
  
          return $dropdown;
      }
  </code></pre>
  
  <p>And my form is on this page:</p>
  
  <pre><code>&lt;?php
  session_start();
  include("includes/func.class.php");
  
  $dob = $func-&gt;fetchDDMMYYYYDropdown('dob_d', $_SESSION['dob_d']);
  ?&gt;
  &lt;form action="t35t_send.php" method="get"&gt;
  &lt;?php echo $dob;?&gt;
  &lt;input type="submit" value="send"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>And it goes to this to save the SESSION variable:</p>
  
  <pre><code>session_start();
  $_SESSION['dob_d'] = $_GET['dob_d'];
  $dob = $_SESSION['dob_d'];
  echo $dob;
  </code></pre>
  
  <p>I can tell that the $_SESSION['dob_d'] is correct and saved as i can output it inside both the function and the initial form page - so it's just the following which must not be right but at the moment the dropdown box just resets back to the first value, not the saved session:</p>
  
  <pre><code>if($session_d==$name){
      $session = 'selected';
  }
  $dropdown .= '&lt;option value="'.sprintf("%02d", $name).'" selected="'.$session.'"&gt;'.sprintf("%02d", $name).'&lt;/option&gt;';
  </code></pre>