e107help.org Q&A
0 like 0 dislike

What is wrong here? I need to know if a user in a given user class is. So far, that has always worked:

if(USER == TRUE)
        {
            // in welchen Benutzerklassen ist der User
            $userClass = $sql -> select("user", "user_class", "user_id = '".USERID."'");
            $userClassUser = $sql -> fetch($userClass);
            // welches ist die Benutzerklasse der Moderatoren
            $moderatorClass = $sql -> select("hra_conf", "hra_conf_userclass", "hra_conf_id = 1");
            $moderatorClassMod = $sql -> fetch($moderatorClass);
            $modClass = $moderatorClassMod['hra_conf_userclass'];
            // gucken, ob Moderatorenklasse in den Userklassen des Users enthalten ist
            if(in_array($modClass, $userClassUser))
            {
                $userModerator = 1;
            }
            else
                {
                    $userModerator = 0;
                }
        }

Here I added a few controls:

echo "UserClassen = ".print_r($userClassUser);
        echo "<br />Moderatoren-Klasse = ".$modClass;
        echo "<br />Moderator = ".$userModerator;

This I become to see:

Array ( [user_class] => 2,3 ) UserClassen = 1
Moderatoren-Klasse = 3
Moderator = 0

Why? Moderator should be 1, not 0. I am completely baffled at the moment.

closed
in Plugins by (45 points) 3 4 5

2 Answers

0 like 0 dislike
Best answer

I don't fully understand what you are trying to do given that you retrieve data from the ''hra_conf" table which I assume is a plugin. 

e107 has an in-built function to check if a user belongs to a specific userclass:  check_class(). 
It can be found in /class2.php 

Simplest way is to have only one parameter, the class you want to check. Say you want to check if the user belongs to userclass ID 2, you'd do this:
 

if(check_class('2'))
{
    echo "User belongs to class";
}
else
{
    echo "User does not belong to class"; 
}

You can also insert more parameters. E.g. when you have a a custom userclass list, you can use the second parameter to insert a list (array I think) of all possible values. 

Hope this helps.  

by (2.8k points) 7 10 10
selected by
In the table "hra_config" is written, which is the user class of "moderator" (here it is 3). This class must be included in user_class the table "user" so that certain things are only the moderators displayed. I think your answer makes me continue. Thank you very much.

Yes, so it works, many thanks.

$moderatorClass = $sql -> select("hra_conf", "hra_conf_userclass", "hra_conf_id = 1");
            $moderatorClassMod = $sql -> fetch($moderatorClass);
            $modClass = $moderatorClassMod['hra_conf_userclass'];
            if(check_class($modClass))
            {
                $userModerator = 1;
            }
            else
            {
                $userModerator = 0;
            }

You're welcome!
You could improve your code by doing this:

 

$modClass = e107::getDb()->retrieve("hra_conf", "hra_conf_userclass", "hra_conf_id = 1"); 

if(check_class($modClass))
{
    $userModerator = 1;
}
else
{
    $userModerator = 0;
}

// or shorthand if statement: $userModerator = (check_class($modClass)) ? 1 : 0;

0 like 0 dislike
I don't know if this helps, but have you triued calling for userclass_classes entity (table)? To know what User class a user has have you look in the admin area?
by (459 points) 17 37 50
In the array represents user class 2,3. The user class that needs to be contained is 3. So should be Moderator = 1. The classes in the array are correct - are mine. I can also make it with e107 :: user, is the same.
996 questions
1,389 answers
2,527 comments
2,512 users