View Full Version : Help with dynamic navigation menu called through php.


Skatiechik
01-06-2005, 13:43
Right I am starting to get more involved on a website and am starting to do some more updates on it.

However I want to change the way it is structured....

Currently the navigation menu is in every html file (loads of them) so everytime you want to change it you have to change loads of files.

Now I want to be able to take the navigation html out of the file and save it in its own right, and call it back into the index pages through the use of the 'includes' function so I will only ever have to change one file to upload the navigation bar through the whole site.

Sounds simple doesn't it, well it is for static navigation menus.

I am confused how to do it for dynamic menus, at this point I should say I know zero php or javascript, and limited html..

The menu structure is similiar to this

Front Page:

Insurance
About Us

Say you click on Insurance

So the menu now looks like this

Insurance
...............Company A
...............Company B
About Us

Hence it being dynamic, now I know I have to introduce a variable but I haven't worked out how to do it.

Can anyone help me please :help:

Phanerothyme
01-06-2005, 14:03
Ok,

have a look here - http://www.hotscripts.com/JavaScript/Scripts_and_Programs/Navigation/Menus/index.html

this is a collection of Javascript navigation scripts. I've not checked through them, but you should be able to find one that suits your purposes.

-- had a look - this one http://www.hotscripts.com/Detailed/42628.html seems to be up your street.

What you also need to do is to create this menu in a file of its own (say menu.htm) and include it in all your site pages.

I would do this using php.

Firstly save your menu script as a file with a php extension (menu.php)

then rename your other pages with .php extensions too

then in each file, remove the existing menu replace it with the code:

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


Now every time your page is accessed, the PHP parser will grab the menu and insert it into the page html.

If you change the menu, the change will be reflected across all you pages since they are all using the same file.

If you are using dreamweaver, you could try using templates which would achieve much the same effect.

There are more elegant ways, but this would be a quick and dirty solution with little or no coding required.

You will need to have a php parser enabled on your webserver.

To check whether you have or not, simply create a file "phpinfo.php', upload it to your normal webspace and request it in your browser (http://yourwebsite.tld/phpinfo.php).

Put this code in the file

<?php
phpinfo();
?>


That will, if php is installed, give you more information about PHP than you need. If its not, you will just get the code.

good luck

Skatiechik
01-06-2005, 14:37
Thanks for that , I did have a look a look around for some scripts last week, and they all seem to be standalone scripts that create different stlye navigation menus which isn't what I am looking for.

Mine needs to stay exactly the same way as it is designed now, I just want it to be handled differently behind the scenes.

I have got it working statically the same way you have shown with the coding below but it needs to work dynamically which is the trouble I am having. I have been told I need to pass a variable into the navigation html to tell it whether it should open the insurance menu or not. This is where I get stuck :confused:

Phanerothyme
01-06-2005, 14:48
oh i see, you want each page to have a different submenu upon opening - the submenu that corresponds to the choices on that page.

using if...then conditionals and testing the $SERVER[PHP_SELF] variable for the name of the page, you could create a menu that knew which page it was being displayed on and it would then unpack the required submenu.

If maybe you could post your menu code here we could have a bash. If you create a menu the way you want it , in a table for simplicity's sake, although its not really the way forward. Create the menu with all the submenus visible. It should be pretty easy to then hide the submenus conditionally with PHP

Skatiechik
01-06-2005, 15:06
Right so for the front page the index menu is coded like this (I can't fnd a VB code button for html so used php button instead for highlighting the code) I have ommitted the top and bottom bits which aren't off too much relevance.

<BR>&nbsp; <IMG height="10" alt=""
src="next011.gif" width="10" border="0"> <A
href="url>Insurance</A>
<BR>&nbsp; <IMG height="10" alt=""
src="next011.gif" width="10" border="0"> <A
href="url">About Us</A>


and say for the insurance.html file the menu looks like this

<BR>&nbsp; <IMG height="10" alt=""
src="next012.gif" width="10" border="0"> Insurance
<BR>&nbsp; <IMG height="10" alt=""
src="next027.gif" width="22" border="0"> <A
href="url">Company A</A>
<BR>&nbsp; <IMG height="10" alt=""
src="next027.gif" width="22" border="0"> <A
href="url">Company B</A>
<BR>&nbsp; <IMG height="10" alt=""
<BR>&nbsp; <IMG height="10" alt=""
src="next011.gif" width="10" border="0"> <A
href="url">About Us</A>


There are loads of submenus on the navigation bar, but for simplicity I have just put two up here.

Phanerothyme
01-06-2005, 15:49
I've not tested this-


<?php
$pagename=$SERVER[PHP_SELF];
?>

<BR>
&nbsp;<IMG height="10" alt="" src="next012.gif" width="10" border="0"> Insurance <BR>
<?php
if ($pagename = 'insurance.php') {
?>
&nbsp; <IMG height="10" alt="" src="next027.gif" width="22" border="0"> <A href="url">Company A</A><BR>
&nbsp; <IMG height="10" alt="" src="next027.gif" width="22" border="0"> <A href="url">Company B</A><BR>
<? } ?>
&nbsp; <IMG height="10" alt="" src="next011.gif" width="10" border="0"> <A href="url">About Us</A>



something like that maybe?

Skatiechik
01-06-2005, 22:34
Thanks for that, obviously it is too late to try it tonight, but I will see if I can find time tomorrow evening or the weekend. Busy busy busy :(

Some questions about it, as I would prefer to know what was happening rather than just aimlessly applying it.

What do these two bits do?

<?php
$pagename=$SERVER[PHP_SELF];
?>

and <? } ?>

Thanks again :) I did post this on a Php forum last week, but no-one responded. Sheffield Forum is great :clap:

Phanerothyme
01-06-2005, 22:54
Originally posted by Skatiechik
Thanks for that, obviously it is too late to try it tonight, but I will see if I can find time tomorrow evening or the weekend. Busy busy busy :(

Some questions about it, as I would prefer to know what was happening rather than just aimlessly applying it.

What do these two bits do?

<?php
$pagename=$SERVER[PHP_SELF];
?>

That bit creates a variable called "pagename" from the contents of the slot in the server variable array that holds the name of the page being parsed

and <? } ?>

That simply closes off the curly brackets from the earlier if() { statement

so....

<!-- html headers content etc in here -->
<?php
//php script tag opens
if ($pagename='insurance.php') { // this line checks to see if pagename is set to insurance php, and if it is it evaluates whatever lies between the curly brackets
//the next line closes the php tag and starts normal html again
?>
now we can put html content in here <br>
<!-- and now we reopen the php tag -->
<?
//close the curly bracket opened after the "if" statement
}
//and now close the php script and go back to ordinary html again
?>

Skatiechik
02-06-2005, 08:46
Thanks ever so much, will give it a try and *cross fingers* it will work :)

Phanerothyme
02-06-2005, 09:06
Well, see how it goes, and post the results!

gl

Skatiechik
07-06-2005, 15:08
Ok it works kinda, however the submenu is permanently open, how do I get it so it only opens when I click on the top level link?