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

Austin Ritz
Austin Ritz
1,580 Points

Php "Nav" Help

I have been trying to figure out how to use PHP to allow my nav list items change color while selected. I have added

<?php $page = 'contact'; ?>

.... contact is replaced with the name of each page.... To the top of each page (above the doc type). and

<li><a href="contact.php" <?php if ($page =='contact') {class ="selected"} ?>>Contact</a></li>

to each link in my header.php file. But I keep getting an error that tells me there is an unexpected "=" in this line and won't run the page. I don't understand if I am missing something or just put something in the wrong spot.

Here is snap shot of my workspace. https://w.trhou.se/oaypyou7xx

1 Answer

Codin - Codesmite
Codin - Codesmite
8,600 Points

You need to echo 'class="selected"' when the IF statement is true. You can change the class using PHP like above but you can echo text into class declaration.

<?php 
   $page = 'contact'; 
?>

<li><a href="contact.php"<?php if ($page == 'contact') { echo ' class="selected"'; } ?>>Contact</a></li>

This will output the following if $page == 'contact' :

<li><a href="contact.php" class="selected">Contact</a></li>