PHP: Displaying different menus to different user types.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TunaSaladBB
Forum Newbie
Posts: 1
Joined: Wed Oct 27, 2010 8:32 pm

PHP: Displaying different menus to different user types.

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

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

Post 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.
atsa1
Forum Newbie
Posts: 20
Joined: Wed Dec 09, 2009 9:11 am

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

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