hello guys.i have an issue.i want to show certain tabs based on user level.
my user levels are:
master
admin
supervisor
gp
patient

But it an admin might be a gp also.how am i supposed to do that?

Member Avatar for diafol

I'd have these levels built on bits:

master = 16
admin = 8
sup'vr = 4
gp = 2
pat. = 1

SO if storing levels for an user:

if a gp and a master - value = 2 + 16 = 18

store this in your users table

then when user logged in you can check if they are a gp and should see the gp tab like so:

if($row['level'] & 2){
  ..show gp tab...
}

if($row['level'] & 16){
  ..show master tab ..
}

etc.

The beauty of this is that you can keep on adding new levels without having to mess up your DB structure.

I'd have these levels built on bits:

master = 16
admin = 8
sup'vr = 4
gp = 2
pat. = 1

SO if storing levels for an user:

if a gp and a master - value = 2 + 16 = 18

store this in your users table

then when user logged in you can check if they are a gp and should see the gp tab like so:

if($row['level'] & 2){
  ..show gp tab...
}

if($row['level'] & 16){
  ..show master tab ..
}

etc.

The beauty of this is that you can keep on adding new levels without having to mess up your DB structure.

though didn't ask the question, I got lost as I was reading :)

Member Avatar for diafol

though didn't ask the question, I got lost as I was reading :)

Sorry I don't understand ev.
I'll elucidate...

If you look at programs like phpBB, they store user levels like this - one field in DB (integer). So one number can represent a number of different level (or rights).

master = 16
admin = 8
sup'vr = 4
gp = 2
pat. = 1

An user who is a GP and also the system admin would have the user level 2 + 8 = 10
This number is unique for that combination of user rights. 10 cannot be made from any other combination of user types. Likewise the site master who is also a supervisor would have 16 + 4 = 20 value.

If at any time the system needs to be updated to include a 'nurse practitioner' or a 'locum doctor', you just do this:


nprac = 64
locum = 32
master = 16
admin = 8
sup'vr = 4
gp = 2
pat. = 1


The bitwise operator (http://php.net/manual/en/language.operators.bitwise.php) '&' is then used to compare whether the user has a certain level:

if you store the values to variable names, e.g.

$master = 16; 
$admin = 8;
$supvr = 4; 
$gp = 2;
$pat = 1;

(although you'd probably store these in the same DB under usertype table or similar).

when you come to display certain bits of the page for different users, you can do this [assume the current user's level is stored in $row['userlevel'] which would be taken from the DB:

if($row['userlevel'] & $master){
  echo "You are a MASTER!!!";
}

If $master (16) is included in the $row, say (20), it will return true, otherwise it will return false.

Sorry if that was obvious. Perhaps there are better ways? E.g. having a user_levels table:

user_id | user_type

where each user can have multiple user_types

commented: that is new to me! +13

Ardav,
It makes sense to me now especially that bitwise operation!

Thank you very much ardav.Your idea was simple and practical.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.