Steve2000 Posted July 28, 2007 Share Posted July 28, 2007 Hi all, I'm fairly new to PHP, and as my first project I'm creating a member site, and I'm currently in the process of making an update profile page. I've got everything like username, password, email etc working, but I have become stuck on the user status. Basically, I have two status options that the user can choose from a drop down menu, Active and Inactive. I want the page to detect which status the current user is, and make that the default selected option in the list. So for example, if a user is active, and they go to the update profile page, the active option is selected for them in the menu. They should be able to change it also. I have no real idea how to do this. I suppose it's a matter of just <? echo $status; ?>, but I don't know how to put this into a drop down menu. Any help on this would be very much appreciated. Thanks. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 28, 2007 Share Posted July 28, 2007 You'd do something like this: <?php // place the code here that defines $status $options = array('active', 'inactive'); echo "<select name=\"status\">\n"; foreach($options as $option) { $selected = ($status == $option) ? ' selected="selected"' : ''; echo ' <option value="' . $option . '"' . $selected . '>' . strtoupper($option). "</option>\n"; } echo "</select>"; ?> That code will automatically have Active or Inactive selected from drop menu depending on the value of $status. Quote Link to comment Share on other sites More sharing options...
Steve2000 Posted July 28, 2007 Author Share Posted July 28, 2007 Thanks for the quick reply wildteen88, that works prefectly! But one more question. I want to do the same thing with user levels. At the moment, the user levels are 1,2,3 and 4 - the actual ranks for these are user, staff, moderator and admin respectively. If I did the same thing here, instead of showing 1,2,3 and 4 as the options in the menu, could I have user, staff, moderator and admin as the values? Kind of like <option value="1">User</option>. Would that be possible? Thanks again. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 28, 2007 Share Posted July 28, 2007 <?php $ranks = array( 1 => 'user', 2 => 'staff', 3 => 'moderator', 4 => 'admin' ); echo "<select name=\"rank\">\n"; foreach($ranks as $k => $rank) { // change $userlevel to the variable that holds the user level id (1, 2, 3 or 4) $selected = ($userlevel == $k) ? ' selected="selected"' : ''; echo ' <option value="' . $k . '"' . $selected . '>' . ucfirst($rank). "</option>\n"; } echo "</select>"; ?> Quote Link to comment Share on other sites More sharing options...
Steve2000 Posted July 28, 2007 Author Share Posted July 28, 2007 Wow, you certainly know your PHP! All this seemed beyond me when I was trying it. Thanks for the help wildteen! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.