Page 1 of 1

Option Selected from SQL

Posted: Tue Feb 22, 2011 7:15 am
by Aleroth
Hello

I just want to know, is it possible to make a <option> be selected according to the value in a mysql database, for example:

i have in my database "month" and its value is "3" and i want the <option> with the same value as the "month" to be selected and the other 11 must still show in the <select> list just the one <option> must be selected. Making sense? Hope so :P

any help?

Re: Option Selected from SQL

Posted: Tue Feb 22, 2011 9:57 am
by AbraCadaver
Yes, show some code for how you query and loop through the data. In general:

Code: Select all

$month = 3;

for($i=1; $i<=12; $i++) {
  $selected = ($i == $month) ? 'selected' : '';
  echo '<option $selected value="'.$i.'">something</option>';
}