cdoyle Posted April 1, 2010 Share Posted April 1, 2010 I'm not sure if this is really a PHP or HTML question, but hopefully it's a simple one. Basically I have a page, that displays a specific person's information. In my user_table I have a field labeled as 'active' 0 for not active 1 for active. So on this page, I want to create a yes/no dropdown and have it display the current status. So I started to create a simple html dropdown, but I'm not sure how to get it to display the current status as the default.. <select name="active"> <option value="1">Yes</option> <option value="0">No</option> </select> $getstaff1['active'] <<< it will either be a 1 or a 0. This is the variable from my query that is ran when the page is opened. I'm not sure how to use this, to make the dropdown display the correct status. Thanks Link to comment https://forums.phpfreaks.com/topic/197259-default-value-of-dropdown-from-query/ Share on other sites More sharing options...
andrewgauger Posted April 1, 2010 Share Posted April 1, 2010 <select name="active"> <option value="1" <?php if($getstaff1['active']==1) echo "SELECTED"?>>Yes</option> <option value="0" <?php if($getstaff1['active']==0) echo "SELECTED"?>>No</option> </select> Link to comment https://forums.phpfreaks.com/topic/197259-default-value-of-dropdown-from-query/#findComment-1035382 Share on other sites More sharing options...
zeodragonzord Posted April 1, 2010 Share Posted April 1, 2010 Perhaps this can help. Not the best code I've ever written but it should work in your case. $select_field = "<select name='active'>"; if(getstaff1['active'] == 1) { $active_1 = " selected"; $active_0 = ""; } else if(getstaff1['active'] == 0) { $active_1 = ""; $active_0 = " selected"; } else { $active_1 = ""; $active_0 = ""; } $select_field .= " <option value='1' $active_1>Yes</option> <option value='0' $active_0>No</option>"; $select_field .= "</select>"; andrewgauger definitely has the better answer if you're able to do inline PHP. Link to comment https://forums.phpfreaks.com/topic/197259-default-value-of-dropdown-from-query/#findComment-1035384 Share on other sites More sharing options...
cdoyle Posted April 1, 2010 Author Share Posted April 1, 2010 <select name="active"> <option value="1" <?php if($getstaff1['active']==1) echo "SELECTED"?>>Yes</option> <option value="0" <?php if($getstaff1['active']==0) echo "SELECTED"?>>No</option> </select> thank you! I knew it had to be something simple like that! Link to comment https://forums.phpfreaks.com/topic/197259-default-value-of-dropdown-from-query/#findComment-1035391 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.