Jump to content

[SOLVED] Drop Down Menus


Steve2000

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/62184-solved-drop-down-menus/
Share on other sites

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.

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.

<?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>";

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.