johntp Posted October 2, 2008 Share Posted October 2, 2008 I have a dropdown menu, and i am trying to get it to set the selected option as whatever the user selected and stored into the database. so say we have 3 options test, blah , and crap. If they chose test it would save to the database, and then when they edit their info i call it and have it as the selected dropdown, but they can change options in the dropdown. I know how to call it, and display it but i dont know how to set it as the default options <select name="Location"> <option value="test">test</option> <option value="blah">blah</option> <option value="crap" selected>crap</option> </select> Is there a really easy way to do this? I tried If else statements and they dont work, and i messed around with a switch but not really sure how to do those. Any help would be appreciated. Link to comment https://forums.phpfreaks.com/topic/126803-solved-pull-default-value-from-mysql-with-drop-downs/ Share on other sites More sharing options...
revraz Posted October 2, 2008 Share Posted October 2, 2008 I would loop the options and have selected as a variable, as you loop and write the options, and check to see if it matches, if so, then set the variable to equal "selected". Link to comment https://forums.phpfreaks.com/topic/126803-solved-pull-default-value-from-mysql-with-drop-downs/#findComment-655887 Share on other sites More sharing options...
johntp Posted October 2, 2008 Author Share Posted October 2, 2008 I kinda figured it out. It's alot fater than i had hoped but if anyone else needs this answer here you go. <?php $dropDownList = array ( "test", "blah", "crap" ); $result = mysql_query("SELECT `Section` FROM `News` WHERE `RECNO`='$RECNO';"); $dataArray = mysql_fetch_array($result); echo "<select name=\"status\">\n"; foreach($dropDownList as $listItem) { if ($dataArray[0] == $listItem) { echo "<option value=\"{$listItem}\" SELECTED>{$listItem}</option>\n"; } else { echo "<option value=\"{$listItem}\">{$listItem}</option>\n"; } } echo "</select>"; ?> Link to comment https://forums.phpfreaks.com/topic/126803-solved-pull-default-value-from-mysql-with-drop-downs/#findComment-655894 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.