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

 

 

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.