Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Build a Simple PHP Application Creating the Menu and Footer Adding Active States to the Navigation

so I did the tutorial correctly but when I go back to the index page the links are both highlighted / underlined

when going to the index page the code has the highlights to all links and a white div background on the shirts link.

2 Answers

Hi Ryan,

This is because the $section variable is undefined on the index page but the nav scripts are still running on that page. php outputs a notice level error informing you about the use of an undefined variable. This error is output where the scripts are which is right inside your class attribute.

This means each part of that message is interpreted as a class name by the browser and it just so happens that some style rules end up matching and you get the unintended styling that you see.

One solution is to just set the $section variable to something on the index page even though you're not going to use it. You have to remember to do this on every page though.

The solution I used was to update the scripts to first check if the variable is set first before using it.

<li class="shirts<?php if ( isset($section) && $section == "shirts") { echo " on"; } ?>"><a href="shirts.php">Shirts</a></li>
<li class="contact<?php if ( isset($section) && $section == "contact") { echo " on"; } ?>"><a href="contact.php">Contact</a></li>
Csaba Koles
Csaba Koles
4,644 Points

This has even helped my problem!

Thank you Jason!