Jump to content

Recommended Posts

Before anything, here's my table layouts.

 

permissions
    perm_id
    perm_desc
    perm_name
    perm_key
    perm_min_level
menu_items
    item_id
    perm_id
    item_key
    item_dname
user_permissions
    uperm_id
    user_id
    perm_id
    perm_level

 

Now, 'permissions' is a table that stores permission settings for various parts of the site.  'menu_items' is where menu links are retrieved from.  Users will see menu items depending on their level.  'user_permissions' is a table with settings which overwrite their user_level.  So if a user has a user_level of 2, but I want to grant them access to something requiring 3, but only to that particular thing, it would go here. 

 

So I need the query to pull their menu items from the database, accounting for the possible overwrite values from user_perms.  I have the following query which is almost what I need, but lacks the overwriting aspect, which I can't figure out :(

 

SELECT m.item_dname as name, m.item_key as key
FROM menu_items as m
    LEFT JOIN permissions as p USING (perm_id)
    LEFT JOIN user_permissions as up USING (perm_id)
WHERE '$user_level'>=p.perm_min_level

 

Do I need two separate queries for this or is there a way to squeeze it all in one?

 

p.s. Thank You Daniel for your join tutorial. 

Link to comment
https://forums.phpfreaks.com/topic/160993-left-join-question/
Share on other sites

First of all, key is a MySQL reserved word.

 

You'll need the user_id for this, but you don't need the $user_level variable assuming that your user's table has a user_level column.

 

This isn't tested.

SELECT mi.item_key as `key`, mi.item_dname as name
FROM menu_items mi
JOIN users u ON u.user_id = 4
JOIN permissions p ON mi.perm_id = p.perm_id
JOIN user_permissions up ON p.perm_id = up.perm_id
WHERE u.user_level >= p.perm_min_level

Link to comment
https://forums.phpfreaks.com/topic/160993-left-join-question/#findComment-849740
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.