msaz87 Posted November 19, 2009 Share Posted November 19, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/182209-restrict-drop-down-options-based-on-user-permissions/ Share on other sites More sharing options...
cags Posted November 21, 2009 Share Posted November 21, 2009 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/182209-restrict-drop-down-options-based-on-user-permissions/#findComment-962662 Share on other sites More sharing options...
seksislav Posted November 22, 2009 Share Posted November 22, 2009 $locations = array(1=>'USA',2=>'UK',3=>'Canada'); $permissions = array(1, 3); echo '<select name="location">'; foreach($locations as $k=>$v) { if(in_array($k, $permissions)) { echo '<option value="' . $k . '">' . $v . '</option>'; } } echo '</select>'; Quote Link to comment https://forums.phpfreaks.com/topic/182209-restrict-drop-down-options-based-on-user-permissions/#findComment-963097 Share on other sites More sharing options...
cags Posted November 22, 2009 Share Posted November 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/182209-restrict-drop-down-options-based-on-user-permissions/#findComment-963107 Share on other sites More sharing options...
msaz87 Posted November 22, 2009 Author Share Posted November 22, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/182209-restrict-drop-down-options-based-on-user-permissions/#findComment-963109 Share on other sites More sharing options...
kickstart Posted November 22, 2009 Share Posted November 22, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/182209-restrict-drop-down-options-based-on-user-permissions/#findComment-963123 Share on other sites More sharing options...
cags Posted November 22, 2009 Share Posted November 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/182209-restrict-drop-down-options-based-on-user-permissions/#findComment-963125 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.