...

View Full Version : Menu on every page



Guero
02-23-2008, 03:58 PM
I have created a menu that I want to use one many pages. I have the style sheet linked. However, is there a way to like the actual content of the menu so that if i want to change a link I don't have to go to every page and edit it.

effpeetee
02-23-2008, 04:14 PM
Could you not just call the one menu page up from every page. My site has many pictures to view, each picture when clicked returns one to the index page. Something like this may help.

www.exitfegs.co.uk

Have a look at it. It won't take long.

Frank

zfred09
02-23-2008, 04:21 PM
This can be done using a php function and an include. In another file use code like this to make the function.



<?php
function menu(){
echo 'put your menu html here';
}
?>


Then in any page you want to display the menu on simply use code like this.



<?php
include "path to file where the above code is";
?>

Then where you want to display it
<?php
menu();
?>


And thats all.

harbingerOTV
02-23-2008, 04:38 PM
or a tad simplier:


<?php include("menu.php"); ?>


and just in case heres an asp way:


<!--#include file ="menu.asp"-->

maim9
03-04-2008, 12:03 PM
Hey,
Sorry for pulling back old posts, but I searched and found this post interesting. However, I'm new to PHP and was wondering if anyone could give help me out a couple of basic questions:

- How should the html code look like inside the php-file? Can I include <html>, <head> and <body>-tags, or should I skip this?
- Can I link to styling sheets inside the php-file?
- How should the code be structured? Do I need to write all html-code on one single line, or can I use my "normal" (tree-)structuring of code?

maim9
03-04-2008, 12:43 PM
Ooops, wrong forum section. Sorry!

jlhaslip
03-04-2008, 07:20 PM
Hey,
Sorry for pulling back old posts, but I searched and found this post interesting. However, I'm new to PHP and was wondering if anyone could give help me out a couple of basic questions:

- How should the html code look like inside the php-file? Can I include <html>, <head> and <body>-tags, or should I skip this?
- Can I link to styling sheets inside the php-file?
- How should the code be structured? Do I need to write all html-code on one single line, or can I use my "normal" (tree-)structuring of code?
1. Skip the html, head and body tags if the 'calling' page already has them.
2. Yes, you can add any html or php in the included file that would normally work in the 'calling' page. Includes are treated as html unless you set the php tags inside it to parse some php.
3. as per above, the included file will be handled as an html snippet, so write the html exactly as you would on a regular page.

SerenitiSFV
03-04-2008, 08:46 PM
- How should the html code look like inside the php-file? Can I include <html>, <head> and <body>-tags, or should I skip this?
- Can I link to styling sheets inside the php-file?
- How should the code be structured? Do I need to write all html-code on one single line, or can I use my "normal" (tree-)structuring of code?

The answers are no, don't skip the tags, yes, you can link to a style sheet, yes you can use your "normal" code structure.

Using an include for just the menu like Harbinger suggested (you just locate it in your page where you'd normally put all the menu code) is a great, simple solution. But what if you have more than just the menu that has to be consistent?

Here's how I do it.

First, create your page layout like you would a template - no content that will differ from page to page, just layout and structure; you know, the stuff that has to be the same on all the pages. Then, highlight the code from the very top down to where your main content goes and save it in a file called something like "header01.php" - even if it doesn't have any PHP code in it.

header01.php:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>IPOonRamp Back Office Login</title>
<link href="cms.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrapper">
<div id="header">
<h1 id="h1Main">Database &amp; Content Management</h1>
<h2>IPOonRamp.com</h2>
<h3>Back Office</h3>
</div>
<div id="mainContent">

Then do the same from the bottom up to the main content.

footer01.php:


</div>
<div id="footer">
<p>All content &amp; images &copy;copyright 2008 The Amerigo Companies.<br />
All Rights Reserved.</p>
</div>
<div id="validation">
<p>
<a href="http://validator.w3.org/check?uri=referer"><img src="http://www.w3.org/Icons/valid-xhtml10-blue" alt="Valid XHTML 1.0 Transitional" /></a>
<a href="http://jigsaw.w3.org/css-validator/"><img src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!" />
</a>
</p>
</div>
</div>

</body>
</html>

Finally, surround what's left with PHP-includes.

index.php:


<?php include("includes/header01.php"); ?>
<form style=";" method="post" action="index.php">
<p>Username:<br />
<input name="username" type="text" /><br />
<br />
Password:<br />
<input name="password" type="password" /><br />
<br />
<input name="submit" type="submit" value="Log In" />
</p>
</form>
<?php include("includes/footer01.php"); ?>

When it opens in a browser (http://www.ipoonramp.info/CMS/), the source code just looks like a plain old static HTML page.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>IPOonRamp Back Office Login</title>
<link href="cms.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrapper">
<div id="header">
<h1 id="h1Main">Database &amp; Content Management</h1>

<h2>IPOonRamp.com</h2>
<h3>Back Office</h3>
</div>
<div id="mainContent">
<form style=";" method="post" action="index.php">
<p>Username:<br />
<input name="username" type="text" /><br />

<br />
Password:<br />
<input name="password" type="password" /><br />
<br />
<input name="submit" type="submit" value="Log In" />
</p>
</form>
</div>
<div id="footer">

<p>All content &amp; images &copy;copyright 2008 The Amerigo Companies.<br />
All Rights Reserved.</p>
</div>
<div id="validation">
<p>
<a href="http://validator.w3.org/check?uri=referer"><img src="http://www.w3.org/Icons/valid-xhtml10-blue" alt="Valid XHTML 1.0 Transitional" /></a>

<a href="http://jigsaw.w3.org/css-validator/"><img src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!" />
</a>
</p>
</div>
</div>

</body>
</html>


The advantage to something this basic is that you could have 63 pages on your web site that have to all look the same except for the main content - header, menu, footer. If you want to change the menu, or something in the header or footer, you only have one place where you need to edit that info and all 63 pages will change. And you don't even need to know PHP except for the syntax for an include. This simple trick is easier and works better than a Dreamweaver template! :thumbsup:

By the way, I can't take credit for this trick - I learned it from Kevin Skoglund's PHP tutorial at Lynda.com (no, I'm not THAT Lynda).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum