0

I am trying to create a page that shows all users with a selection of details. One of those details is the user role it has. For each role I want to display a different string. In order to achieve this is have the following code:

function smoelenboek_func(){
    $users = get_users('orderby=nicename');
    foreach ($users as $user) {
        switch ($user->roles[0]) {
            case 'administrator' :
                echo 'Yes';
            case 'member' :
                echo 'No';
            case 'oldmember' :
                echo 'Maybe';
        };
    };
};

Now the strange thing that happens is, users with an administrator role get output "YesNoMaybe", a member role gets "NoMaybe" and oldmember gets "Maybe". Am I using the Switch-Case incorrectly? Is there something in the roles array that I'm misinterpreting? Any help is hugely appreciated.

0

You missed break statement

This is how switch case works

switch ($i) {
    case "apple":
        echo "i is apple";
        break;
    case "bar":
        echo "i is bar";
        break;
    case "cake":
        echo "i is cake";
        break;
}
|improve this answer|||||
-1

Your code is missing break and also your requirement is like below:

If administrator, YesNoMaybe OR if member, NoMaybe OR if oldmember, Maybe.

Finally code is look like Below:

function smoelenboek_func(){
    $users = get_users('orderby=nicename');
    foreach ($users as $user) {
        switch ($user->roles[0]) {
            case 'administrator' :
                echo 'YesNoMaybe';
                break;
            case 'member' :
                echo 'NoMaybe';
                break;
            case 'oldmember' :
                echo 'Maybe';
                break;
        };
    };
};

I hope this may help you well.

P.S. Let me know if you have any query/concern regarding this.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.