0

I am creating an admin menu for a site and I have several drop down lists that need to be created for each different field with the default selected option being different for each record that appears (this is very similar idea to the permission admin panel in phpBB). In other words, I have a table that lists all of the groups that a user could belong to, and in each row there are select menus for changing the permissions of that group. I am currently using functions for each different set of menus that looks like this:

function rfiPermissions($x){
$query = "SELECT rfi_permission FROM groups WHERE id = '$x'";
$result = mysql_query($query) or die();
$row = mysql_fetch_array($result);
$perm = $row[0];
$option = array();
$option[0] = "<option value='0'>None</option>";
$option[1] = "<option value='1'>Basic</option>";
$option[2] = "<option value='2'>Supplemental</option>";
$option[3] = "<option value='3'>Full</option>";
switch($perm){
    case 0:
        echo $option[0].$option[1].$option[2].$option[3];
        break;
    case 1:
        echo $option[1].$option[0].$option[2].$option[3];
        break;
    case 2:
        echo $option[2].$option[1].$option[0].$option[3];
        break;
    default:
        echo $option[3].$option[1].$option[0].$option[2];
        break;
}
}

I have been trying to figure out a more stream-lined way of doing this because I need to implement this many more times in my site, but I have been unable to do so. Is there a better way of doing this where I can simply pass an array of what the options need to be into a function, then echo the one value from the db, remove it from the array, then echo the rest of the options?

1 Answer 1

1

It is easier to set the selected attribute with php than to change the order. Plus, as a user I like consistency and if I looked at a drop down and the order changed each time I looked at it I would be frustrated.

$option[0] = "<option value='0' ".($perm==0 ? " selected " : "") .">None</option>";
$option[1] = "<option value='1' ".($perm==1 ? " selected " : "") .">Basic</option>";
$option[2] = "<option value='2' ".($perm==2 ? " selected " : "") .">Supplemental</option>";
$option[3] = "<option value='3' ".($perm==3 ? " selected " : "") .">Full</option>";

This is done in if short hand and can be expanded for readability

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.