brem13 Posted March 10, 2010 Share Posted March 10, 2010 hey, i have a profile page with options to edit, and i was wondering how i would go about having the options pre-selected if they are already in a mysql database? <select name="hair"> <option name="brown">Brown</option> <option name="blonde" selected>Blonde</option> <option name="black">Black</option> <option name="red">Red</option> <option name="brunette">Brunette</option> <option name="auburn">Auburn</option> <option name="gray">Gray</option> <option name="white">White</option> <option name="other">Other</option> <option name=""></option> </select> like if someone already filled out the profile page and their hair color was gray, how would i have it automatically go to that in the drop down???? here is the mysql code mysql_connect($server, $db_user, $db_pass) or die (mysql_error()); $result = mysql_db_query($database, "select * from $table WHERE email = '$email'") or die (mysql_error()); while($qry = mysql_fetch_array($result)){ $gender = $qry['hair']; }//end while Link to comment https://forums.phpfreaks.com/topic/194803-select-option-pre-filled-from-mysql-database/ Share on other sites More sharing options...
wildteen88 Posted March 10, 2010 Share Posted March 10, 2010 I would setup up an array of all hair colors, eg $hair_colors = array('brown', 'blonde', 'black', 'red', 'brunette', etc etc); I'd then use a foreach loop to dynamically generate the drop down menu and have a condition inside to it which compares the current color with that in the database. If there is a match add selected="selected" to the <option> tag. echo '<select name="hair_color">'; foreach($hair_colors as $color) { $selected = (($color == $qry['hair']) ? ' selected="selected"' : ''); echo '<option value="'.$color.'"'.$selected.'>'.$color.'</option>'; } echo '</select>'; Link to comment https://forums.phpfreaks.com/topic/194803-select-option-pre-filled-from-mysql-database/#findComment-1024351 Share on other sites More sharing options...
brem13 Posted March 11, 2010 Author Share Posted March 11, 2010 thank you very much, it works! however i'm just wondering if you could briefly explain this line of code, i dont know what the ? and : are for? $selected = (($color == $qry['hair']) ? ' selected="selected"' : ''); Link to comment https://forums.phpfreaks.com/topic/194803-select-option-pre-filled-from-mysql-database/#findComment-1024816 Share on other sites More sharing options...
wildteen88 Posted March 11, 2010 Share Posted March 11, 2010 The ?: is called a ternary operator, basically an inline version of an if/else statement. The above can written as if($color == $qry['hair']) $selected = ' selected="selected"' else $selected = ''; Link to comment https://forums.phpfreaks.com/topic/194803-select-option-pre-filled-from-mysql-database/#findComment-1024915 Share on other sites More sharing options...
brem13 Posted March 12, 2010 Author Share Posted March 12, 2010 ok, cool, thanks for your help!! Link to comment https://forums.phpfreaks.com/topic/194803-select-option-pre-filled-from-mysql-database/#findComment-1025213 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.