Jump to content

[SOLVED] need help comparing array's


mike16889

Recommended Posts

i'm making an image gallery for a few other cms's/forums and i want to limit certain gallery's to certain user groups. i was simply going to have some check boxes so the admin can check the boxes of the user groups and then it would be stored in a table like this: 1, 2, 5, 8 and so on, and then just use the explode function to place them into an array. then i would simply do an in_array comparing the user's class and the permissions in the gallery, but the problem is when a user is a few different userclasses there userclass number ends up like this: 1, 4, 8 excreta.

 

is there any easy way to compare 2 array's and give a true (or similar) output if any of the array contents match?

 

p.s. I'm still a beginner...

Link to comment
Share on other sites

You shouldn't store data like that

 

Instead you should use three tables, one for users, one for userclasses and one for relations between them

 

table users
user_id,name
1    ,foo
2    ,bar
3    ,baz

table classes
class_id, name
1      ,classFoo
2      ,classBar
3      ,classBaz

table users_classes
user_id,class_id
1        ,1
1        ,2
1        ,3
2        ,2
3        ,2
3        ,3

 

In this example user foo (user_id 1) has access to all classes, user bar has only access to classBar, and user baz has access to classBar and classBaz.

 

If you wan't to check what classes user belongs to, you just query

 

SELECT class_id FROM users_classes WHERE user_id = $user_id

 

If you want to check if user belongs to specific  class you do like this

SELECT class_id FROM users_classes WHERE user_id = $user_id AND class_id = $class_ID

And if the query returns no rows, then sorry dear user, you;re not allowed to see that ;)

Link to comment
Share on other sites

@Mchl = as i said i'm adding this onto another system that allready has its usertables and stuff set up and i;m trying to keep this as small and simple as i can.

 

@sasa = i did'nt actualy end up using your solution but it got me thinking. i have now made a small scrip that works, here it is:

 

$fpmmG_userclass = "1,2,3";
$fpmmG_userclassarray = explode(",", $fpmmG_userclass);
$fpmmG_permissions = "4,5";
$fpmmG_permissionsarray = explode(",", $fpmmG_permissions);

$fpmmG_permissionsdiff = array_diff($fpmmG_permissionsarray, $fpmmG_userclassarray);

if($fpmmG_permissionsarray == $fpmmG_permissionsdiff){
echo 'Sorry you can\'t';
} else {
echo 'You Have Permssion';
};

 

thanx anyway.

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.