Should I use php or css to have a navigation menu template?

I have uploaded a personal site containing around 10 pages using html, photoshop, css, php and mysql.

I have used css for my layout, positioning of header image, columns etc. But I have written the code for my navigation menu on every page. This was not so difficult as my site did not contain too many pages.

Now I want to learn how to create a template for a navigation menu too so that I just need to change one single page whenever I add new pages to my site.

I need the advice of my sitepoint friends to find out the best way to go about it. Should I use php or css? Which is easier to use? Can I add the html code for the navigation menu in my css file itself? Does using a php include have any added advantage? Which path should I follow along my learning curve?

You’ll use both PHP and CSS.

They serve different purposes. CSS affects how it looks. PHP affects how things work.

A simple way to start templating your site is to first pull out a common section (in this case, your navigation). Move this into a separate file (such as header.php). Then you’ll want to put an include (or require) where you took the navigation out of each page.

You’ll then use CSS to style the HTML used to make the navigation.

PHP includes (and requires) basically just copy and paste anything in the included file where you have the include. If the file you’re including is PHP, it’ll also cause that PHP to be processed. The only difference between include and require is include triggers a warning when it fails, required triggers an error (causing the site to stop loading).

Thanks, samanime

The only difference between include and require is include triggers a warning when it fails, required triggers an error (causing the site to stop loading).

In that case, since I just want to have a file which contains my navigation menu, it may be better to use an include file and use the require only if there is something more important like a php file, is it? To actually learn more about the usage of require and include, I will study in detail later, but for now, I just wanted to know whether most of you would just use an include file for a simple navigation menu.

Well, for navigation it’s kind of a toss-up.

You’ll probably want to use an include, but you’ll also want to make sure you’ll always be there.

You must need PHP to do this cause you need something like storing your menu items in a page and then call it in all the pages in which way you can have consistent menu in all the pages and changing in that single page which contains the menu items.
Try “include” function in PHP.

PHP is the most simple way of doing this. I use it all the time in the templates that I make.

Like stated before, all your links and such in one file, then include it in a page. If you change a link, you change it once instead of on all the pages.

Thanks for the inputs.

I have just incorporated a php include file for the navigation menu in al the pages of my personal site and thanks to all of you, it is working succesfully, I am now planning to use include files for the header and footer as well. I read in one of the tutorials that we can use the include file for the header to incorporate information such as the doctype, call to css file, meta declaration etc. So, I thought of trying that out too by using something like

<title><?php echo $page_title; ?></title>

in my header include file.

But before doing that, I just wanted the advice of the pros. As a newbie, am I complicating things for myself by deciding to include the other features like title, call to css file etc in the header include file? Is that what most of you pros do? Or as a newbie, should I just stick to writing the code for a simple header, footer and navigation menu in 3 separate include files and type out the doctype info, call to css file, meta tag info etc. in all the pages as I go along creating them?

Let’s take a step back for a section.

First, plan out your site.

Then, after that, figure out which elements will be common throughout all of your pages. Those parts are what you want to pull into separate files. Each continuous block can go into one file, then be included in to each of your other files.

A typical site will usually break up the header, navigation (which may be in the header file), and footer.

Just keep in mind that using includes essentially copies and pastes anything you have in the include file into the location you put the include function and it makes more sense. Nothing magical or unpredictable, just copying and pasting. =)

Not quite true.

PHP includes and requires will include the contents of the named file. That file can contain a set of variables or constants that you want to use, and a set of functions you want to use. All in one file. (although I do keep my database variables in a second include file)

You can then call which ever variable, constant or function you want at a latter stage. It could be a function to start building the head, another one to insert a css file or javascript, another one to close your head, another one to create your menu, another one to create your footer. All in a single file.

It is true that many newcomers use includes to include just their menu or just their footer, and yes, that is a simpler thing to do. But an include does NOT
"just copy and paste anything " by any stretch of the imagination.

for example


<?php
include 'general.php';

/* database bits removed for brevity */

$title="Folk Festivals in Scotland";
makeHeader2($title, 'Scotland');

insertCSS('folkfestival.css');
insertCSS('menu.css');
echo "<script type=\\"text/javascript\\" src=\\"$topup\\"></script>";
echo "<script type=\\"text/javascript\\" src=\\"http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js\\"> </script>";
insertScript('accordian.js');
closeHeader('two'); /* two to make current page  in css */
				
?>
<div id="topper">
<h1>UK Folk Festivals - Scotland</h1>
	
</div>
	
  <div id="header" > 
  <p>Where the music grows</p>
  </div>

  <?php makeMenu(); ?>
  
	<div id="innerwrapper">

Functions like makeMenu() just insert html code, while functions like makeHeader2($title, ‘Scotland’); do a bit more with the values passed to them.

This function makeFooter($thisYear, $email, $address); takes three values from the include file and inserts them at the appropriate points in the footer. $thisYear changes yearly, obviously, and is edited once in the general.php, and is updated in several other places, not just the footer.

And you don’t need to be a php expert to do this sort of thing.

Thanks for your input.

It is true that many newcomers use includes to include just their menu or just their footer, and yes, that is a simpler thing to do. But an include does NOT
"just copy and paste anything " by any stretch of the imagination.

As of now, I am still the newcomer who has used 3 php includes for the header, navigation and footer.

The following is the code for one of my files where I have used the 3 include files. Am I doing it the correct way? Or is it better to use a single file in this case too? But if we use a single include file, I am confused as to how the header, menu, footer etc. can be inserted in the correct locations. I seem to be not able to understand something basic but I am not sure as to what it is.

 
<?php
$page_title = "jppp - My Personal Web Page - Interesting Facts";
$page_description = "jppp - My Personal Web Page - interesting facts";
 
 include("header.php"); ?>
               Centre content goes here.Centre content goes here.Centre content goes here.Centre content goes hereCentre content goes here.
 </div>
 <div id="leftmenu">
<div id="side1">
  <?php include("menu.php"); ?>
 </div></div>
 
 <div id="footer"><br />
<?php include("footer.php"); ?></div>
</div>
</body>
</html>

You can then call which ever variable, constant or function you want at a latter stage. It could be a function to start building the head, another one to insert a css file or javascript, another one to close your head, another one to create your menu, another one to create your footer. All in a single file.

Do you mean that instead of having 3 php include files like I have, I can have just one include file and then call functions to build the head, insert the navigation menu and footer? Since I did not understand it fully, I tried googling for tutorials for the same but did not find a satisfactory one which could help a newbie.

@Dr John: How is that not the same affect as copying and pasting? What does it do that copying and pasting wouldn’t do? If you copy and paste, all those functions and variables get defined just the same. Behind the scenes it is different, but on the surface it is essentially the same, so it is a decent way to explain to beginners includes work.

Back on topic, jppp, it looks like what you are doing is an appropriate method to include your files.

Some people like to define functions to generate and build their code (which is what Dr John eluded to). However, for simple HTML content it makes just as much sense to do it the same way you did.

When you start learning about functions and classes you’ll have additional uses for included files, but for what you are doing now you’re doing it in a good way.

Underlying all of this the main goal is to just never have to write something twice, because then you have to maintain it in two places.