Jump to content

[SOLVED] PHP Navigation


Veej

Recommended Posts

Hello.

I have a website with 3 pages:

Home.php

About.php

Contact.php

 

On each page there are 3 navigation buttons; 'Home'; 'About'; 'Contact'

 

When you are on the home page, the 'Home' button will be blue and the about and contact buttons will be red.

 

When you are on the about page, the 'About' button will be blue while the home and contact buttons will be red.

 

When you are on the contact page, the 'Contact' button will be blue and the home and about buttons will be red.

--

 

What I want to do is to have just one file: menu.html

This file will be pulled in on all pages using the PHP include function.

 

If I wanted to display the same menu on each page, this would be fine. However, I want to display different coloured menu buttons on each page.

 

I hope I have described my problem clearly and will appreciate any help!

 

Thanks.

Link to comment
Share on other sites

If I am correct you want one menu file and depending on what page is open you want the menu button to that page being in a diferent state to the rest?

 

Just before you include() the menu file in have a variable set, something like this:

$page='about';

 

Have something like that set on every page to include the menu which will also have to be a PHP file, not HTML as you need to have some code executing inside it, something like this:

<?php
  if ($page=='home') {print '<a href="index.php">Home</a><br />';} else {print 'Home<br />';}
  if ($page=='about') {print '<a href="about.php">About</a><br />';} else {print 'About<br />';}
  if ($page=='contact') {print '<a href="contact.php">Ciontact Me</a><br />';} else {print 'Contact Me<br />';}
?>

 

That's the basic idea, hope it explains well enough.

 

EDIT: Forgot to add br at the end of else statements.

Link to comment
Share on other sites

Thanks Yesideez,

I'll try that way for now and get back to you.

 

Do you know if there are other ways though as even with this, if I want to make one change to a button, I will have to make 3 edits in the PHP file.

 

Not a major worry but would be great if there was another way!

 

Thanks again for the help, much appreciated!

Link to comment
Share on other sites

There is a way using arrays which would cut down the code (you'd only have to edit a couple arrays instead of code)

<?php
  $pageID=array('home','about','contact'); //Used to check against $page which is set in each script
  $pageText=array('Home Page','About','Contact Me'); //The text to appear as the menu items
  $pageFile=array('index','about','contact'); //The file names of the scripts
  for ($i=0;$i<count($pageID);$i++) {
    if ($page==$pageID[$i]) {
      print '<a href="'.$pageFile[$i].'.php">'.$pageText[$i].'</a><br />';
    } else {
      print $pageText[$i].'<br />';
    }
  }
?>

 

That's the basic idea. Just have to edit the 3 arrays instead of adding code and you'd still need to define $page in each page before the menu is included. Can't guarantee this code will work first time as it's off the top of my head.

Link to comment
Share on other sites

Thanks Yesideez, I've done it a slightly different way.

 

The source code is below in case anyone else is in the same position.

This way is also quite easy to update because if I need to change any of the buttons, I know where to go.

 

1. The code on the actual page webpage

<html>
<link rel="stylesheet" href="images/style.css">
<?php
$page=='home';
$rootaccess = "";

include ($rootaccess."navigation.php"); ?>

 

The stylesheet link is just so the proper formatting is applied - you can remove that if you don't have a stylesheet in that location.

The include just links to the navigation file which I made with the guidance of Yesideez.

 

2. The code of navigation.php

<html><STYLE TYPE="text/css">
<!--
  CSS Declarations go here.
-->
</STYLE>

<div id="nav">
<ul>
<?php
//Home Page
if ($page=='home') {print '<li id="current"><a href="http://url.com/index.php">Home</a></li>';}
else {print '<li><a href="http://url.com/index.php">Home</a></li>';}

//Blog OR Review Page
if ($page=='blog') {echo '<li id="current"><a href="http://url.com/blog">Reviews</a></li>';}
else {echo '<li><a href="url.com/blog">Reviews</a></li>';}

//About Page
if ($page=='about') {echo '<li id="current"><a href="http://url.com/about.php">About Us</a></li>';}
else {echo '<li><a href="http://url.com/about.html">About Us</a></li>';}

?>

</ul>
</div>
</html>

The div id just controls the design which I've defined in the 'CSS Declarations found here' area.

If you'd like to see this in action, it can be found at www.takeitfromteens.com/index.php

 

Thanks Yesideez :)

 

Link to comment
Share on other sites

Glad to have been of help ;)

 

The only reason I opted to use print instead of echo when building the menu is to make the output HTML easier to read - handy when debugging later if needed.

 

btw, there isn't really any need to specify entire URLs in your links. Even if you're in a subfolder of the server you can use a single forward slash (/) prefixing the script filename to point to the root folder.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.