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

Ben Brenton
Ben Brenton
266 Points

What is wrong with this PHP code?

I'll try and be as specific as possible as I have not included all of the code. What I am trying to do is to create a navigation bar which highlights in a color the page that you are currently on. Using an include file, the following is a conditional PHP statement which should, in my mind do this. The code I haven't added is the "selected" value which is the correct tag in my CSS file which changes the color of the current page to blue. Also, I have given each page a PHP variable $page = "";.

I hope this is specific enough. Can anyone explain why this may not be working?

        <li<?php if ($page == "index") { echo "selected"; } ?>><a href="index.php">Home</a></li>

5 Answers

Niclas Valentiner
Niclas Valentiner
8,947 Points
<li<?php if ($page == "index") { echo "selected"; } ?>>

The above code would, assuming $page is index, generate the following html:

<liselected>

I believe you see the problem now? Php tags and white space don't show up in the html and I'm guessing you want "selected" to be a class too.

Jeff Lemay
Jeff Lemay
14,268 Points

Take out the php statement and replace it with what you are trying to echo, "selected".

<liselected><a href="index.php">Home</a></li>

You've just created a < liselected > element!

You want to put the php inside a class attribute.

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

So now if your if statement is true:

<li class="selected"><a href="index.php">Home</a></li>
Ben Brenton
Ben Brenton
266 Points

Thank you for the suggestions!

I'm afraid the code still isn't behaving properly. Niclas Valentiner you seem to be the closest I think the problem does lie in the "li" tag not working correctly. I have tried all suggested methods however and none seem to be working, some even render the entire function unworkable. It looks like the real issue is that the code isn't performing the conditional statement action properly.

Niclas Valentiner
Niclas Valentiner
8,947 Points

You can always add a check variable that you leave empty and change the value of before, inside and after the conditional then just echo it out in plain text somewhere unobtrusive on the page. Like inside your index.php link. Should help identify what parts of the code runs and what doesn't.

It's easier to do in javascript for example since you can just alert it or console.log() what you're looking for but a test variable works just fine too.

Hmm, make sure you have the $page variable set. You can either check it's set inside the conditional:

<?php

if (isset($page) && $page == 'index') { 
    // do stuff
}

Or make sure it's always being set in your code. Please right click and 'inspect' the html element to make sure the correct mark up is being printed.

Hope this helps!

You must add "on" (or w/e you want to call it) class to 'li' if it's the page you are on as in the following PHP code:

<li class="<?php if ($page == "index") { echo "on"; } ?>">

so the following CSS code must be

li.on a{
    color: blue;
}