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

Marc Jenkinson
Marc Jenkinson
554 Points

$echo not returning desired .class style

Trying to use what we learned in "Shirts 4 Mike" by using $echo to apply "on" but it seems to not be working for me.

In my page.php I have:

<?php 
    include ('inc/header.php'); 
    $section = "work";
?>

and in my header.php I have:

<li class="right<?php if ($section =="work") { echo "active"; }?>">
    <a href="index.php">Work</a>
</li>

Any ideas where my problem is? It just isn't rendering the css (which worked fine before). My css files don't have to be .php as well do they?

Your code didn't post correctly. This link has some tips on how to do that. https://teamtreehouse.com/forum/how-to-type-code-in-the-forum

See if you can post your code that has the echo statements.

Your css files should end in .css

2 Answers

Include can be used like a statement or a function with paremeters (like you are using it now). Meaning with parenthesis or without:

http://www.php.net/manual/en/function.include.php

http://stackoverflow.com/questions/4955732/is-phps-include-a-function-or-a-statement

But if you do use the parentheses, there should not be a space between include and the first parenthesis.

The address to your stylesheet. "/main.css"(or whatever) is in header.php yeah?

EDIT: Also, I missed this too

<!-- Below returns class="rightactive" But you want "right active"-->
<li class="right<?php if ($section =="work") { echo "active"; }?>">
    <a href="index.php">Work</a>
</li>

<!-- should be like be this to return a proper "right active" notice the space before I type out "active" -->
<li class="right<?php if ($section =="work") { echo " active"; }?>">
    <a href="index.php">Work</a>
</li>

Your css is in your header.php right? This should resolve the head portion of you document which is the underlying problem.

In addition to not having a space between the class names as Leonardo pointed out, I would recommend that you use the isset() function in your conditionals like so:

<li class="right<?php if ( isset($section) && $section =="work") { echo " active"; }?>">

If you have any pages where you don't set the $section variable either because you forgot or you don't need it on that page then that script is going to generate a php error because you are checking if an undefined variable is equal to something. That error will be inserted into your class attribute. Then each part of the error message is going to be interpreted as a class name by the browser.

See this post for more details if interested: https://teamtreehouse.com/forum/creating-the-menu-and-footer-render-error