Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Kelly Ferrell
Kelly Ferrell
2,561 Points

background color lost on the shirt NAV icon

I created an underline, that is activated when the user selects the shirt link. It works great but once you click it and then go bak to the homepage the shirt nav background in no longer orange but white.

6 Answers

I think this is something to do with defining the active variable on the homepage. I've got a feeling Randy leaves this part out - you just need to define the $section is on all pages. I think... It was a long time ago!

Hi Kelly,

The solution I went with is to make sure the variable is set first before trying to use 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>

This way you don't have to worry about defining the $section variable on pages that don't use it.

Paul Roberts
Paul Roberts
7,928 Points

Similar to what Tom, Jason and Sherry have already written...

I tried to keep it as close to Randy's as possible:

<ul class="nav">

<li class="shirts<?php if ($section == "shirts") { echo " on"; } ?>"><a href="shirts.php">Shirts</a></li>
<li class="contact<?php if ($section == "contact") { echo " on"; } ?>"><a href="contact.php">Contact</a></li>
<li class="cart"><a href="#">Shopping Cart</a></li>

</ul>```

Also, it's important to define $section at the top of index.php:

          <?php 
          $pageTitle = "Unique T-shirts designed by a frog";
          $section = "index";
          include('inc/header.php'); 
          ?>

Hope this helps!

Kelly Ferrell
Kelly Ferrell
2,561 Points

Thanks Tom, Your right. Defining the $section varable on the homepage worked.

Kelly

Sherry Parker
Sherry Parker
9,601 Points

Thanks Jason! I tried setting the $section on the homepage and that didn't fix it, but your isset solution worked for me.

Hi guys. Had the same problems.

So really defining the $section = "index.php"; in the index.php file just like Paul mentioned is really important.

Now for the problem with the white color behind the Shirts icon, I fixed it by making sure that the <?php opening tag had a space <?php_ after the opening PHP element in:

<?php 
$pageTitle = "Mike's Full Catalog of Shirts"; 
$section = "shirts";
include('inc/header.php'); ?>

Guess those small little things really make PHP totally unforgiving in comparison to HTML. So heads up and keep up to the standards! ;)