Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

Christoph Kähler
Christoph Kähler
1,333 Points

Why do we need to create an extra $section variable for echoing out the " on"?

Why can't we just use the $pageTitle variable for echoing out the " on" class in the HTML to underline the categories? I tried it out and it seems to be working just as fine (I only needed to start with an upper case letter - like Music - as we echo it out that way in the page title in the categories).

Is it only because one would have to change the page title here as well if one wanted to change it and thus cause unnecessary effort? Or is there another reason I don't see?

Additional question: I tried to put the page titles in extra variables, like $books = "Books" and then setting $pageTitle == $books; , which also seems to work fine, however when I write the if-statements in the navigation list items like this:

<li class="movies <?php if ($pageTitle == $movies){echo " on"; } ?>"><a href="catalog.php?cat=movies">Movies</a></li>

, all of a sudden all categories are underlined when I click the home button, whereas this does NOT happen when I do not use these category variables and write out the page title itself (i.e.:

<li class="books <?php if ($pageTitle == "Books"){echo " on"; } ?>"><a href="catalog.php?cat=books">Books</a></li>).

Thanks for your help in advance!

3 Answers

Tim Knight
Tim Knight
28,863 Points

Hi Christoph,

I'd say that setting a separate value just let's your conditional check be a little cleaner. For example, what if your title was more complex than just a single word? Something like, "Fiction and Non-Fiction Books". That keeps me from having to write a crazy long conditional comparison.

In terms of setting one as the other, you can do that temporarily if you want but when you check your conditional it's always going to be the same (which is why all of your categories are being marked as on). Here's what I mean:

<?php
  $pageTitle = "Books";
  $sectionTitle = "books";

  if $pageTitle == $sectionTitle {
    // will always evaluate as true. Because they are the same.
  }
?>

So when you're actually checking the navigation you should check it's actual value, because if your just checking to see i the values are the same as each other they'll always evaluate to true. Make sense? Instead you'd actually want to write:

<?php
  <?php
  $pageTitle = "Books";
  $sectionTitle = "books";

  if $sectionTitle == 'books' {
    // I'm a book.
  }
  if $sectionTitle == 'movie' {
    // I'm a movie.
  }
?>

Hi Christoph,

In the general case, these 2 variables serve different purposes. The $section variable is used to control logic on the page and the $pageTitle variable sets the document title which shows up in search results. So you want the ability to set the page title to whatever is best for seo and the $section variable to something short that describes that page. Also, if your page title changes later, you don't want to also have to go into your code and make changes relating to the page logic.

In this case the variables almost have the same value but it's common for the document title to contain what the page is about along with the website name or company name. For the books page, the title could be "Books | Personal Media Library" as an example.

For your additional question, you didn't specify but I'll assume you set those extra variables in the catalog.php. That's the only thing I can think of to get the results you were getting. I'll have to revise my answer if this isn't the case.

First, I want to say you're not really gaining anything by adding the extra variables. Plus if more categories are added then you have to create more variables.

For what happened on the homepage, your extra variables that you created in catalog.php will be undefined on the homepage. This means that you'll get an undefined variable notice for each list item that's trying to use an undefined variable. This notice gets output where the script is which is right inside the class attribute.

This means that every word in that notice gets treated as a class name.

A typical notice looks something like this: Undefined variable: user_location in C:\wamp\www\mypath\index.php on line 12 Yours will be different.

You can see that the word "on" is in there so you've effectively given every list item the class of "on" leading to all of them being underlined.

Christoph Kähler
Christoph Kähler
1,333 Points

Hi guys, thanks for your answers and sorry for my late reply! This really helped a lot.