Page 1 of 1

php menu question

Posted: Tue Mar 10, 2009 11:31 pm
by phi0x
Hi guys! I am having trouble with a menu problem, I wanted to make my friends site that I'm working on for portfolio work, more dynamic so I can edit it easier, one thing he likes me doing frequently is changing the menu bar links around.

I converted the site to php and made an include statement in all my pages that use the menu bar so that I don't have to go through each page and edit the menu bar manually. (I thought this was smart..)
I soon find out that the menu bar links, don't look the way they used to, what i mean by that is this:

http://www.mountainlifeadventures.com/Home.php

you see the home link is set to have a different background colour then the other links?
I want it where if you're on lets say the 'howitworks' page that it changes the links colour just like the Home link is set.


I guess my overall question is, how do you code it so that when someone clicks on other links that the class that changes the colour of the link for the page which you are on, actually changes.

Ryan.

Re: php menu question

Posted: Wed Mar 11, 2009 1:12 am
by requinix
Put some logic in the menu file that decides where the user is and which button should be highlighted.

For instance, if the URL (look at $_SERVER[REQUEST_URI]) ends with "Home.html" then the Home button should be active, "Howitworks.html" for the How It Works page, and so on.

Re: php menu question

Posted: Wed Mar 11, 2009 1:14 am
by phi0x
I am very newb and this is what I had done for the menu.php file that is included into every page with the following statement:

Code: Select all

include 'menu.php';
here is the menu.php file code:

Code: Select all

 
 
<div class="content-header">
    
    <div class="invertedshiftdown2">
<ul>
<li class="current"><a href="Home.php" title="Home">Home</a></li>
<li><a href="Howitworks.php" title="How it works">How it works</a></li>
<li><a href="Canada.php" title="Canada">Canada</a></li>
<li><a href="Japan.php" title="Japan">Japan</a></li>
<li><a href="Applicants.php" title="Applicants">Applicants</a></li> 
<li><a href="Employers.php" title="Employers">Employers</a></li>
<li><a href="Gallery.php" title="Gallery">Gallery</a></li>
<li><a href="Sponsor.php" title="Sponsored Links">Sponsored Links</a></li>
<li><a href="Contactus.php" title="Contact Us">Contact Us</a></li>
</ul>
</div>
<br style="clear: both;" />
    
    
    </div>
 
 
I'm a bit confused still on how to make it work -_-

Re: php menu question

Posted: Wed Mar 11, 2009 1:30 am
by requinix

Code: Select all

<li class="current"><a href="Home.php" title="Home">Home</a></li>
See that "class=current"? You don't want it there all the time so you need to decide if and where it should appear.

Code: Select all

'<li' . $class_home . '><a href="Home.php" title="Home">Home</a></li>'
Repeat that same kind of thing for all the buttons.

Now determine what $class_home, $class_howitworks, $class_canada, and all the others should be. One of them will be ' class="current"' and the rest will be empty.
A change from what I said before: look at $_SERVER[SCRIPT_NAME] to decide which is which. basename can give you the name of the file, stripping out the rest of the string. Then compare it to "Home.php", "Howitworks.php", and the others; whichever one matches gets the added HTML.

Code: Select all

// first, set all the $class_ variables to an empty string
$class_home = $class_howitworks = $class_canada = "";
// then determine which shouldn't be and change it
$file = basename($_SERVER["SCRIPT_NAME"]);
$add = ' class="current"';
if ($file == "Home.php") $class_home = $add;
else if ($file == "Howitworks.php") $class_howitworks = $add;
else if ($file == "Canada.php") $class_canada = $add;
There are better ways to do this, but this method is simple and easy to understand. For the time being it should be sufficient.

Re: php menu question

Posted: Wed Mar 11, 2009 2:16 am
by phi0x
thank you, this helped! I had a bit of trouble outputing the html but I figured out with an echo statement I could wrap all of the html and output it :)

This would have been way beyond my comprehension so far, thanks for the help, it makes sense and I have learned a bit! I will study it some more once I am complete the updates for my friend. :)