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
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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.