title: Select dropdown value from variable
body: <p>I wanted to have a dropdown's selected value be determined by the results of the a query. But whatever I try, its selected value stays at the very first option.</p>
  
  <p>I've browsed multiple questions with the same problem, but none solved my problem.</p>
  
  <p>I've tried this:</p>
  
  <pre><code>&lt;select name="Period"&gt;
      &lt;option value="Day" &lt;?php if($PeriodTXT == "Day") echo "selected"?&gt;&gt;Day&lt;/option&gt;
      &lt;option value="Week" &lt;?php if($PeriodTXT == "Week") echo "selected"?&gt;&gt;Week&lt;/option&gt;
      &lt;option value="Month" &lt;?php if($PeriodTXT == "Month") echo "selected"?&gt;&gt;Month&lt;/option&gt;
      &lt;option value="Year" &lt;?php if($PeriodTXT == "Year") echo "selected"?&gt;&gt;Year&lt;/option&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>And this:</p>
  
  <pre><code>&lt;select name="Period"&gt;
         &lt;option &lt;?php echo ($PeriodTXT == 'Day')?"selected":"" ?&gt; &gt;Day&lt;/option&gt;
         &lt;option &lt;?php echo ($PeriodTXT == 'Week')?"selected":"" ?&gt; &gt;Week&lt;/option&gt;
         &lt;option &lt;?php echo ($PeriodTXT == 'Month')?"selected":"" ?&gt; &gt;Month&lt;/option&gt;
         &lt;option &lt;?php echo ($PeriodTXT == 'Year')?"selected":"" ?&gt; &gt;Year&lt;/option&gt;
   &lt;/select&gt;
  </code></pre>
  
  <p>But it won't work. Also when I use <code>echo "$PeriodTXT";</code> it echos "Week" (exactly as written in the options of the dropdown), so it should've selected "Week" but it doesn't.</p>
  
  <p>EDIT: <code>$PeriodTXT</code> is supposed to show the selected interval that came with a number count (example "3 Day" it would only keep the "Day" part)</p>
  
  <pre><code>    $usersdata = array();
      while($row = mysqli_fetch_array($result))
      {
          $usersdata = $row;
      }
  
      $PeriodTXT = preg_replace("/\d+/u", "", $usersdata[4]);
  </code></pre>