Jump to content

User role based menu function


frc

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/246217-user-role-based-menu-function/
Share on other sites

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.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.