Start Tutorial
One way to highlight the current page your on is by using php and that's what were be using today. What were going to do is remove the link to stop people trying to click on it again by mistake and then make the text bold so it's obvious where they are.
All you need todo is include topNav.php into all your pages which you want included in your navigation and then set the variables in topNav.php.
Directory tree
For the purpose of this tutorial my directory tree looks like this. So if your following along create
- Folders - php and css inside your document root folder
- files - contact.php, index.php and news.php inside your document root folder (Using xhtml strict template)
Step 1.
Save this as topNav.php and place it in your PHP folder modifying the following variables if desired.
- title text - line 6
- filenames - line 9,15,19
- link text - line 12,16,20
- <?php
-
- $pageName = basename($_SERVER['PHP_SELF'],'.php') ;
-
-
- $title = "Your on this page";
-
-
- $p1 = "index";
-
-
- $p1t = "Home";
-
-
- $p2 = "news";
- $p2t = "Headlines";
-
-
- $p3 = "contact";
- $p3t = "Contact Us";
- ?>
-
- <!-- Start topNav -->
- <div class="topNavContainer">
- <ul class="topNav">
-
- <!-- page 1 -->
- <li>
- <!--
- <?php if ($pageName == $p1) { echo '<span class="noActive" title="'.$title.'">'.$p1t.'</span>'; } else { echo '<a href="'.$p1.'.php">'.$p1t.'</a>'; } ?>
- </li>
-
- <!-- page 2 -->
- <li>
- <?php if ($pageName == $p2) { echo '<span class="noActive" title="'.$title.'">'.$p2t.'</span>'; } else { echo '<a href="'.$p2.'.php">'.$p2t.'</a>'; } ?>
- </li>
-
- <!-- page 3 -->
- <li>
- <?php if ($pageName == $p3) { echo '<span class="noActive" title="'.$title.'">'.$p3t.'</span>'; } else { echo '<a href="'.$p3.'.php">'.$p3t.'</a>'; } ?>
- </li>
-
- </ul>
- </div>
- <!-- End topNav -->
Step 3.
Save this as topNav.css and place it in your CSS folder
- .topNav {
- border:1px solid #000;
- background-color:#FFC;
- width:500px;
- height:30px
- }
-
- .topNav li {
- display:inline;
- list-style-type:none;
- overflow:visible
- }
-
- .topNav a {
- font-family:Arial, Helvetica, sans-serif;
- font-size:1.1em;
- padding-right:1.5em;
- padding-left:1.5em;
- color:#903;
- background-color:#FFC
- }
-
- .topNav a:hover {
- color:#FFF;
- background-color:#903;
- font-weight:700;
- text-decoration:none
- }
-
- .topNav a:active {
- color:#FFF;
- background-color:#006
- }
-
- .noActive {
- font-family:Arial, Helvetica, sans-serif;
- font-size:1.1em;
- font-weight:700;
- text-decoration:none;
- color:#000;
- background-color:#FFC;
- padding-right:1.5em;
- padding-left:1.5em
- }
Step 4.
Insert this in contact.php, index.php and news.php inside the head tags.
- <link rel="stylesheet" type="text/css" href="css/topNav.css" media="screen" />
Now if everything is right your pages should look and work like the
demo
Download
I hope you enjoyed this tutorial and that it was helpful to you in some way.
Download
End Tutorial
Copyright © 2010-2011. Ian.J.Gough All rights reserved.