Forum Moderators: open

Message Too Old, No Replies

same menu on all pages, create it dynamically?

         

kslnor

2:09 pm on May 19, 2007 (gmt 0)

10+ Year Member



I'm not sure if I am using the proper term when I refer to "dynamically.

I want to have a simple, one level, menu displayed on all 80 pages of my site. The menu items will change periodically - new items added, some removed, etc... What is the best way to create this menu so I don't have to update all 80 pages? FYI: I'm not too knowledgeable in all the scripting languages. Thank you.

thecoalman

2:57 pm on May 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Use an include, it's "scripting" but very simple to do. If you have PHP on your server try this, first create mypage.php :

<html>
<head>
<title></title>
</head>
<body>
<?php include($_SERVER['DOCUMENT_ROOT'] . '/somedirectory/mymenu.php');?>
</body>
</html>

Then create mymenu.php which can simply conatian some text, html, whatever....:

TEST

Upload both and make sure mymenu.php is located at somedirectory/mymenu.php

When you load mypage.php in a browser it should have the text "TEST".

PHP is pretty common but if you don't have it you will have some type of server side includes and the directions will be different.

rocknbil

5:49 pm on May 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you don't mind all your pages being php, and want to learn a little programming. You can also do this with server side includes as mentioned. Wherever the menu goes, you insert an SSI directive:

<!--#include file="mymenu.txt" -->

or if mymenu.txt resides in a directory,

<!--#include virtual="/directory/mymenu.txt" -->

Normally this applies only to files with a .shtml extension so the server knows what files to parse to look for SSI commands, and also SSI must be enabled on your server. You can change your server configuration to parse ALL plain text files, or all files with an .htm, .html, or .shtml extension. Have your server administrator set it up, then all you need to do is experiment with SSI's. You can test if SSI's are already active on your server:

1. Create a file with an SSI directive

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>SSI test</title>
</head>
<body>
<p>test</p>
<!--#include file="mymenu.txt" -->
</body>
</html>

Save this file as test.shtml.

2. Create a plain text file

<p>This is my include file</p>

And name it mymenu.txt.

3. Upload them both to the same directory on your server and browse to the page. If you see both "test" and "This is my include file" on the page, SSI's are up and running.

kslnor

9:04 pm on May 19, 2007 (gmt 0)

10+ Year Member



Thanks for both replies, I opted for the non-php way (for some reason ir seems easier for me ;) )

Works great. Is there a limit to the number of "includes" I can have per page?

Thanks again.

thecoalman

3:52 am on May 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Don't let the first part of that code intimidate you, this works too:

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

The difference being that is a relative path and will only work for files in the same directory. The original one I posted is a absolute path and will work on any file no matter what it's location is.

The benefit of using PHP for includes is just the tip of the iceberg as far as what you can do with PHP.