Option Selected from SQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Aleroth
Forum Newbie
Posts: 8
Joined: Tue Jan 25, 2011 7:38 am

Option Selected from SQL

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Option Selected from SQL

Post 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>';
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply