frc Posted September 1, 2011 Share Posted September 1, 2011 I'm not totally new to php but I'm no guru either. I'm building an intranet and have three seperate user roles, user - manager - admin. There are some menu items I don't want to allow simple users to see. This will expand later to give them different views of a page as well (some can view/others can edit). As it stands the login validation is holding they're user level in $SESSION. To give you an idea take a look at what I'm trying to do: function usermenu($usermenu) { if($user_level=0) echo ("<ul id="gooeymenu2" class="solidblockmenu"> <li><a href="main.php">Home</a></li> <li><a href="forms.php">Forms</a></li> <li><a href="/support/index.php" target="_new">Support</a></li> <li><a href="documents.php">Documents</a></li> <li><a href="admin/index.php">Admin</a></li> <li><a href="logout.php">Logout</a></li> </ul> <script> gooeymenu.setup({id:'gooeymenu2', selectitem:1, fx:'swing'}) </script>" "); else($user_level=1,2) echo ("<ul id="gooeymenu2" class="solidblockmenu"> <li><a href="main.php">Home</a></li> <li><a href="forms.php">Forms</a></li> <li><a href="/support/index.php" target="_new">Support</a></li> <li><a href="documents.php">Documents</a></li> <li><a href="new.php">New Adviser</a></li> <li><a href="admin/index.php">Admin</a></li> <li><a href="logout.php">Logout</a></li> </ul> <script> gooeymenu.setup({id:'gooeymenu2', selectitem:1, fx:'swing'}) </script>" "); Any help would be great, thanks in advance. Jason Quote Link to comment https://forums.phpfreaks.com/topic/246217-user-role-based-menu-function/ Share on other sites More sharing options...
noXstyle Posted September 1, 2011 Share Posted September 1, 2011 Hi there, First off, since you aren't exactly new to php you already know that the code won't parse . And yeah, i would create an array with page details and userlevels in it instead of echoing everything separately... To make myself clear i will provide a small snippet for you: function usermenu() { $pages = array( 0 => array( 'page_name' => 'Index', 'page_url' => 'main.php', 'attrs' => null, 'userlvl' => array(1,2,3)), ... ); } And then loop the array: $menu = "<ul id=\"gooeymenu2\" class=\"solidblockmenu\">\n"; foreach($pages as $k=>$v) { if(in_array($_SESSION['userlevel'], $v['userlvl'])) { // matches userlevel from $_SESSION to page's userlvl array $menu .= "<li><a href=\"".$v['page_url']."\""; if(!empty($v['attrs'])) { $menu .= " ".$v['attrs']; // if attributes not empty append to li.. you should format the array list as 'attrs' => "target=\"_blank\"" } $menu .= "/>".$v['page_name']."</li>\n"; } } $menu .= "</ul>"; // and other stuff you need return $menu; } // end func echo usermenu(); That way you can easily generate the menu for each userlevel. But hey, that's just one idea along millions.. Hope that gives you something. Quote Link to comment https://forums.phpfreaks.com/topic/246217-user-role-based-menu-function/#findComment-1264504 Share on other sites More sharing options...
frc Posted September 2, 2011 Author Share Posted September 2, 2011 Thanks for the reply. That's a really good idea! It's probably going to take me a few to figure it out but yay! Jason Quote Link to comment https://forums.phpfreaks.com/topic/246217-user-role-based-menu-function/#findComment-1264729 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.