Page 1 of 1

PHP: Displaying different menus to different user types.

Posted: Wed Oct 27, 2010 8:34 pm
by TunaSaladBB
Hi all,

I'm new to PHP and just looking for any advice anyone might have on the following problem I'm having.

I have created a database and tables for registering and logging in users. Each record has three different User Types (or roles): 1, 2 and 3. Once the session has started, I would like to make certain menu items available to certain User Types. I suppose it is some sort of if...elseif statement such as:

<?php if($UserType==1) echo {"<p>Menu1</p>"}
elseif($UserType==2) echo {"<p>Menu2</p>"}
elseif($UserType==3) echo {"<p>Menu3</p>"}; ?>

Would anybody be able to point me in the right direction? I'd really appreciate any help at all.

Re: PHP: Displaying different menus to different user types.

Posted: Wed Oct 27, 2010 8:37 pm
by Jonah Bron
What have you tried? What you have there looks like a good start. Remember, not displaying the menu options is not enough: you must implement security measures on the pages they link to to make sure the wrong users can't use it.

Re: PHP: Displaying different menus to different user types.

Posted: Thu Oct 28, 2010 3:29 am
by atsa1
I would solve this like that:

Create the menu useing MySQL useing like 4 cells. id (autoinsert), Menutext (shown on page), Link (adress to somewhere) type ( 1 or 2 or 3 or 4).
Every user has it's own type (1 or 2 or 3 or 4).
create session where you take out the user "type"
And then like this:
$type = $_SESSION['type'];
function menu(){
$menu = mysql_query("SELECT * FROM menu WHERE type='$type' order by id");
while ($row = mysql_fetch_array($menu)) {
$name = $row['title'];
$adress = $row['location'];
echo "<a href=\"$adress\">$name</a>";} //*
}
This will take out the menuitems corresponding to the user type.
notice that you have to create the sesion out of the User type for the menu $type.
and then on page use: echo menu(); whereever you want your menu to appare.
you can use table to arrange your menu. just create table and insert echo menu(); in a tablerow.