Best way to manage a Dynamic Site Navigation in PHP?

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

User avatar
Tonkpils
New php-forum User
New php-forum User
Posts: 19
Joined: Tue Aug 19, 2003 6:11 am
Location: An Italian in Brusssels...

Best way to manage a Dynamic Site Navigation in PHP?

Postby Tonkpils » Tue Aug 26, 2003 6:43 am

Hello reader,
I'm building a menu navigation for a secure site, and i'd like to know the best way to develop the script, without being lost in a jungle of codes...
Here's the story:
After logged in, there are 5 different levels of rights on the site. According to the "userLevel" in the database, a user see a different menu to navigate through the site.
Till now everithing works fine and as soon as you go to the index.php you have a menu according to the rights given.
This is for the moment a "single line" menu, with no sub menu. This can be managed in the page itself. But if i want to include this sub menu in the PHP script how can i do it?
Now when you arrive to the page you have the include() fuinction that calls a script i did, where depending of the $UserLevel a menu is displayed (echo ''). Is a single line of html that looks like this:
Home | Technical Documents | Release notes | Issue Tracking | Manuals

Depending of the $UserLevel it can be (for instance) like:
Home | Technical Documents | Release notes
And this is ok for the moment.

If now i wanna add a second line of menu, like if for 'Technical document' there are 2 submenus, what can i do?
I'm thinking to create an hidden value in the page that should have a two menu line, and add to the script another menu on two lines called when (for instance)

Code: Select all

$userLevel == '2' && $title == "technical"

where '$title' is the hidden field in the page.
Is this the right logic?

Thanks to all of you... If is not clear, please tell me... I'm always in a rush !!!! :lol: [/code]

User avatar
ruturajv
php-forum Super User
php-forum Super User
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India
Contact:

Re: Submenu

Postby ruturajv » Tue Aug 26, 2003 10:31 pm

Why don't you just create the include file in such a way that the it would display a sub menu, perhaps you can make a whole big image and recall the image

your problem just seems to be displaying the link , right?? :?:

Joel
New php-forum User
New php-forum User
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand
Contact:

Postby Joel » Tue Aug 26, 2003 10:54 pm

Or you could use some mysql tables

Code: Select all

Table: Menu
- id - e.g. 3 (primary key)
- menuname - e.g. "Technical Documents"
- userlevel - 1 to 5

Table: SubMenu
- id - e.g. 3 (primary key)
- menuid - e.g. 1 (relative to menu id from menu table)
- name - "Tech document sub"


Then you could have sql statements for the nav after a use is logged in

Code: Select all

SELECT * FROM menu WHERE userlevel > $userlevel


And then on the sub menu

Code: Select all

SELECT * FROM submeny WHERE menuid = $menuid

sigix
php-forum Active User
php-forum Active User
Posts: 364
Joined: Mon Jul 14, 2003 9:39 pm
Location: /Earth/Xion

Postby sigix » Tue Aug 26, 2003 11:14 pm

make a function headermenu in your common.php file
like this

<?php

function headmenu(){

?>
all your html and font/...etc setting


<?
}

?>

all the setting html..etc is enclosed in the php headfunctino and when you will call this function it will place the menu there so when you want to make a change you have to change it once and all other pages will be automatically changed :arrow:
[/code]

User avatar
Tonkpils
New php-forum User
New php-forum User
Posts: 19
Joined: Tue Aug 19, 2003 6:11 am
Location: An Italian in Brusssels...

I DIDI IT BUT NOW I DON'T GET THIS MISFUNCTION

Postby Tonkpils » Thu Aug 28, 2003 6:52 am

Ok, i did it but not following the advices yuo gave me... :oops: Will you go on through this now? i hope...
I build the two script to be called via the function "include()", and everithing works fine. But not for the submenu. Where i place the menu i've put the code

Code: Select all

<?php include 'menu.php';?>

The navigation sistem works fine. Here's the file menu.php

Code: Select all

<?php

if ($userLevel == '2'):
    echo '<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="17" bgcolor="A6ADC7" class="MenuPortal"><span class="menuPortalNODHTML"></span><span class="menuPortalNODHTML">&nbsp;
   <a href="index.php" class="menuPortalNODHTML"><strong>Home</strong></a>
    | <a href="release.php" class="menuPortalNODHTML"><strong>Release Documents</strong></a>
   | <a href="issues.php" class="menuPortalNODHTML"><strong>Issue Tracking</strong></a>
   | <a href="technical.php" class="menuPortalNODHTML"><strong>Technical Documentation</strong></a>
   | <a href="manuals.php" class="menuPortalNODHTML"><strong>User Manuals</strong></a>
</span></td></tr></table>';
   
elseif ($userLevel == '3'):
    echo '<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="17" bgcolor="A6ADC7" class="MenuPortal"><span class="menuPortalNODHTML"></span><span class="menuPortalNODHTML">&nbsp;
   <a href="index.php" class="menuPortalNODHTML"><strong>Home</strong></a>
    | <a href="release.php" class="menuPortalNODHTML"><strong>Release Documents</strong></a>
   | <a href="issues.php" class="menuPortalNODHTML"><strong>Issue Tracking</strong></a>
   | <a href="technical.php" class="menuPortalNODHTML"><strong>Technical Documentation</strong></a>
   | <a href="manuals.php" class="menuPortalNODHTML"><strong>User Manuals</strong></a>
</span></td></tr></table>';
   
elseif ($userLevel == '4'):
   echo '<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="17" bgcolor="A6ADC7" class="MenuPortal"><span class="menuPortalNODHTML"></span><span class="menuPortalNODHTML">&nbsp;
   <a href="index.php" class="menuPortalNODHTML"><strong>Home</strong></a>
    | <a href="release.php" class="menuPortalNODHTML"><strong>Release Documents</strong></a>
   | <a href="issues.php" class="menuPortalNODHTML"><strong>Issue Tracking</strong></a>
   | <a href="technical.php" class="menuPortalNODHTML"><strong>Technical Documentation</strong></a>
   | <a href="manuals.php" class="menuPortalNODHTML"><strong>User Manuals</strong></a>
</span></td></tr></table>';
   
elseif ($userLevel == '5'):
   echo '<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="17" bgcolor="A6ADC7" class="MenuPortal"><span class="menuPortalNODHTML"></span><span class="menuPortalNODHTML">&nbsp;
   <a href="index.php" class="menuPortalNODHTML"><strong>Home</strong></a>
    | <a href="release.php" class="menuPortalNODHTML"><strong>Release Documents</strong></a>
   | <a href="issues.php" class="menuPortalNODHTML"><strong>Issue Tracking</strong></a>
   | <a href="technical.php" class="menuPortalNODHTML"><strong>Technical Documentation</strong></a>
   | <a href="manuals.php" class="menuPortalNODHTML"><strong>User Manuals</strong></a>
</span></td></tr></table>';
   
elseif ($userLevel == '6'):
   echo '<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="17" bgcolor="A6ADC7" class="MenuPortal"><span class="menuPortalNODHTML"></span><span class="menuPortalNODHTML">&nbsp;
   <a href="index.php" class="menuPortalNODHTML"><strong>Home</strong></a>
    | <a href="release.php" class="menuPortalNODHTML"><strong>Release Documents</strong></a>
   | <a href="technical.php" class="menuPortalNODHTML"><strong>Technical Documentation</strong></a>
   | <a href="manuals.php" class="menuPortalNODHTML"><strong>User Manuals</strong></a>
</span></td></tr></table>';
   
elseif ($userLevel == '7'):
   echo '<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="17" bgcolor="A6ADC7" class="MenuPortal"><span class="menuPortalNODHTML"></span><span class="menuPortalNODHTML">&nbsp;
   <a href="index.php" class="menuPortalNODHTML"><strong>Home</strong></a>
   | <a href="technical.php" class="menuPortalNODHTML"><strong>Technical Documentation</strong></a>
   | <a href="manuals.php" class="menuPortalNODHTML"><strong>User Manuals</strong></a>
</span></td></tr></table>';
      
endif;

?>

When calling the submenu, i've inserted a veriable in the page itself, to tell the submenu wich option display.
This is the code to call the submenu.php in the page:

Code: Select all

          <?php
        $type = "tech"; //it can be "tech" or "issue" or "manual", depending of the page where it is inserted.
        include 'submenu.php';
        ?>

then the submenu.php is this:

Code: Select all

<?php

if ($userLevel == '2' && $type == 'issue'):

    echo '<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="17" bgcolor="E5E5E5" class="MenuPortal"><span class="menuPortalNODHTML"></span><span class="menuPortalNODHTML">&nbsp;
   <a href="issues.php" class="menuPortalNODHTML">partner 1</a>
    | <a href="issues.php" class="menuPortalNODHTML">partner 2</a>
   | <a href="issues.php" class="menuPortalNODHTML">partner 3</a>
</span></td></tr></table>';

elseif (($userLevel == '3' | '4' | '5' | '6' | '7') && ($type == 'issue')):

    echo '<table width="100%" height="17" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="17" bgcolor="E5E5E5" class="MenuPortal"><span class="menuPortalNODHTML"></span><span class="menuPortalNODHTML">&nbsp;
</span></td></tr></table>';
   
elseif (($userLevel == '2' | '3' | '4' | '5' | '6' | '7') && ($type == 'tech')):

    echo '<table height="17" width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="E5E5E5" class="MenuPortal"><span class="menuPortalNODHTML"></span><span class="menuPortalNODHTML">&nbsp;
   <a href="technical.php" class="menuPortalNODHTML">Order Progress</a>
    | <a href="technical.php" class="menuPortalNODHTML">Integration</a>
</span></td></tr></table>';

elseif (($userLevel == '2' | '3' | '4' | '5' | '6') && ($type == 'manuals')):

    echo '<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="17" bgcolor="E5E5E5" class="MenuPortal"><span class="menuPortalNODHTML"></span><span class="menuPortalNODHTML">&nbsp;
   <a href="manuals.php" class="menuPortalNODHTML">Buyer</a>
    | <a href="manuals.php" class="menuPortalNODHTML">Seller</a>
</span></td></tr></table>';

elseif (($userLevel == '7') && ($type == 'manuals')):

    echo '<table height="17" width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="E5E5E5" class="MenuPortal"><span class="menuPortalNODHTML"></span><span class="menuPortalNODHTML"></span></td></tr></table>';

endif;

?>

The concatenation works for all the submenu, but in the last one i don't see an empty space, but still see the items "buyer" and "seller", but the variable says $userLevel = 7 and $type = manuals. The two items must be visible only if $userLevel = 2 or 3 or 4 or 5 or 6. Not for 7... I'm checking if there is something wrong with the structure of the script, but i don't see anything strange...
Thanks for your help... And if you think the way io write the code is too complicated... tell me. Is only a couple of week that i started coding from scratch... :oops:

User avatar
ruturajv
php-forum Super User
php-forum Super User
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India
Contact:

Script

Postby ruturajv » Thu Aug 28, 2003 9:33 pm

Hi,

I'm sorry to say but the script is a bit too complicated to understand at first sight.

I recommend you that you use the switch control stucture. Because you have too many conditions to go with.

Note for Admin: See this script, it is difficult to read with a single font color green why cant the highlight feature be implemented

User avatar
Tonkpils
New php-forum User
New php-forum User
Posts: 19
Joined: Tue Aug 19, 2003 6:11 am
Location: An Italian in Brusssels...

Postby Tonkpils » Thu Aug 28, 2003 11:55 pm

Yes, i agree that at first sight is not clear, but if you copy and paste it in DreamweaverMX (or other editor) it show up with different color. BTW, i solved it my self, but if you read at my other post http://www.php-forum.com/p/viewtopic.php?t=2921 you see that i didi something but i don't know why it works. If someone can explain... ???
then yes, definitely the switch control is something i should read and use... But i'm moving the first step in PHP (i mean writing something from scratch) since a couple of weeks... AND I LOVE IT !!!

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Re: I DIDI IT BUT NOW I DON'T GET THIS MISFUNCTION

Postby swirlee » Fri Aug 29, 2003 2:39 pm

[quote="Tonkpils"]

Code: Select all

elseif (($userLevel == '3' | '4' | '5' | '6' | '7') && ($type == 'issue'))


This doesn't do what you expect it to do. First of all, use || for OR, not | .. I couldn't even find in the documentation what a single | does, though it is a valid operator. Anyway, the above will always evaluate to true, because in what's happening is it's compaing $userLevel to ('3' | '4' | '5' | '6' | '7'), which always evaluates to true. Instead, you need this:

Code: Select all

elseif(
   (($userLevel == '3') ||
   ($userLevel == '4') ||
   ($userLevel == '5') ||
   ($userLevel == '6') ||
   ($userLevel == '7')) &&
   $type == 'issue
) {
   ...
}


Of course, a more sensible way to do this would be:

Code: Select all

elseif(
   (($userLevel >= '3') && ($userLevel <= '7')) &&
   $type == 'issue
) {
   ...
}


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 2 guests