Jump to content

Restrict drop down options based on user permissions


msaz87

Recommended Posts

I was hoping someone might help me with, or point me in the direction of, a simple way of restricting drop down list options based on a user's permissions.

 

For instance, there are two tables in use, a user table and a locations permission and so if the locations are:

 

ID......Location

1........USA

2........UK

3........Canada

 

And a user's permissions are set up for 1,3, then it would show:

 

<select name="location">
<option>USA</option>
<option>Canada</option>
</select>

 

I'm sure I'm oversimplifying this... but any help is appreciated... thanks!

Link to comment
Share on other sites

Untested, but something along these lines should do the job...

 

$locations = array(1=>'USA',2=>'UK',3=>'Canada');
$permissions = array(1, 3);

echo '<select name="location">';
foreach($locations $k=>$v) {
   if(in_array($k, $permissions)) {
      echo '<option value="' . $k . '">" . $v . '</option>';
   }
}
echo '</select>';

Link to comment
Share on other sites

Rather than just copying my code and fixing a slight typo I made (which is pretty hard to see in your post since you haven't used syntax highlighting) why not just point out I made a typo, including the correct code after if you really need to.

Link to comment
Share on other sites

Untested, but something along these lines should do the job...

 

$locations = array(1=>'USA',2=>'UK',3=>'Canada');
$permissions = array(1, 3);

echo '<select name="location">';
foreach($locations $k=>$v) {
   if(in_array($k, $permissions)) {
      echo '<option value="' . $k . '">" . $v . '</option>';
   }
}
echo '</select>';

 

Hey Cags, I probably should have elaborated more on where everything was coming from... so here is a more thorough explanation:

 

There's going to be a user DB, permission DB and location DB...

 

So the location DB is simple enough, something like:

 

location_idlocation

1USA

2France

3Germany

 

And this DB populates the drop down I'm looking to filter...

 

The User DB:

user_iduser

1matt

2john

3steve

 

and finally the permission DB:

location_iduser_id

11

12

21

23

31

33

 

(so in the above example, user Matt has access to all locations, John to just France and Steve to France and Germany...

 

So currently the code to fill the drop down is just...

 

		<form method="post" action="<? echo($_SERVER['PHP_SELF']); ?>">

		Primary Location:<br/>
		<select name="primary_location">

			<? while($row = mysql_fetch_array($primary_locations)){ ?>

				<option <? if ($primary_location_selection == ($row['location_id'])) echo "selected"; ?> value="<? echo ($row['location_id']); ?>"><? echo ($row['location']); ?></option>

			<? } ?>				

		</select> <input type="submit" value="go">

	</form>	

 

Sorry for not giving this earlier.. thanks so much for the help!

Link to comment
Share on other sites

Hi

 

Use some SQL like this:-

 

SELECT location_id, location
FROM Locations a
INNER JOIN Locations_Users b
ON a.location_id = b.location_id
INNER JOIN Users c
ON b.user_id = c.user_id
WHERE user = 'Matt'

 

(although in real code you would probably be searching for a user id rather than user name).

 

This would give you the list of locations that a user can see

 

All the best

 

Keith

Link to comment
Share on other sites

I'll assume that when you say DB you mean table. Run this query.

 

SELECT l.* FROM location l
JOIN permissions p ON l.location_id=p.location_id
JOIN user u ON u.user_id=p.user_id

 

Then loop through these results outputting them in a manner very similar to what your currently doing.

 

Edit: D'oh.

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.