title: PHP - Fill in select boxes with value form database
body: <p>I've a form with select boxes and a value from database which has been chosen before. Yes I need this value which is chosen to be shown as last chosen inside form. I try using php dom but I' m not getting anywhere. </p>
  
  <p>So here is the html:</p>
  
  <pre><code>&lt;select name="conf_1"&gt;
       &lt;option value="Y"&gt;Yes&lt;/option&gt;
       &lt;option value="N"&gt;No&lt;/option&gt;
  &lt;/select&gt;
  &lt;select name="conf_2"&gt;
       &lt;option value="text"&gt;text&lt;/option&gt;
       &lt;option value="alsotext"&gt;alsotext&lt;/option&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>And now I would like to show the chosen option which is stored in array like this:</p>
  
  <pre><code>$confs = array([1] =&gt; N, [2] =&gt; text );
  </code></pre>
  
  <p>Do some code:</p>
  
  <pre><code>loop{
     &lt;option value="$confs[$i]"&gt;$confs[$i]&lt;/option&gt; has to be appended to &lt;select name="conf_$i"&gt;
  }
  </code></pre>
  
  <p>Now I can't find a way to do this. I've tried using php dom but I don't know how to select element by name attribute and then loop trough so I can't make a real php example. I just would not know where to start.</p>
  
  <p>If someone just has a way to do this please enlighten me!?</p>
  
  <p>Done it with this code inspired by @Markus I.:</p>
  
  <pre><code>$dom = new DOMDocument();
      $dom-&gt;loadHTML($form);
      $xPath = new DOMXPath($dom);
  
      foreach ($dom-&gt;getElementsByTagName('select') as $select) {
          $name = $select-&gt;getAttribute('name');
          $name = str_replace('conf_', '', $name);
  
          foreach($xPath-&gt;query('./option', $select) as $option) {
              if ($option-&gt;getAttribute('value') == $confs[$name]) {
                  $option-&gt;setAttribute('selected', 'selected');
              }
          }
      }
  
      $form = $dom-&gt;saveHTML($dom);
  
      echo $form;
  </code></pre>