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

lyonel scapino
lyonel scapino
14,191 Points

Shirts 4 Mike Project conditional to underline current page fails

when I type this code in my header.php code page I get a white square around the shirts icon: <li class="shirts<?php if ($section == "shirts") {echo "on"; } ?>"> <a href="shirts.php">Shirts</a></li>

If I remove the php part it gets orange correctly again.

7 Answers

lyonel scapino
lyonel scapino
14,191 Points

Indeed, inserting the variable does fix the issue, nice debugging!. Thanks.

lyonel scapino
lyonel scapino
14,191 Points

the underlining works in both cases. The problem is that when I put the conditional statement on the shirts line, it creates a white square around the Shirts image. I agree it could be a css problem but I never touched it, only downloaded it from the attached files of the course. Thus I believe the creators of this course would have seen something.

I think this was missed in the video. I had this problem too and I defined the $section variable in index.php before using it. Like this:

<?php 
$pageTitle = "Unique T-shirts designed by a frog";
$section = "";

include('inc/header.php'); ?>

And so forth. Just make sure that it's done BEFORE it loads header.php.

Yep. Just tested it. I commented out php $section = "" and the white box returned. Apparently you need to declare/initialize the variable before using it.

Thanks Rodger. I was also getting this problem. Could see by View Page Source it didn't like an undefined variable. Didn't think it was ideal to be adding a variable that is effectively nothing, but hey it works!

Stone Preston
Stone Preston
42,016 Points

Can you post the whole conditional you were using. It looks like you may have forgotten to close the php tag like so

?>

But can't be certain without more context

lyonel scapino
lyonel scapino
14,191 Points

```<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>

lyonel scapino
lyonel scapino
14,191 Points
<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>
Stone Preston
Stone Preston
42,016 Points

does the underlining work with the contact link? your php looks pretty good so it might be an issue with the css styling and not the php.