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 Build a Basic PHP Website (2018) Building a Media Library in PHP Adding Active States to the Navigation

Ammu Nair
Ammu Nair
9,100 Points

Class 'on' not getting reflected. Active link not underlined.

Hi, I dont find anything wrong with my code, but the class 'on' does not seem to be added to the <li> link, when the page is active. Posting relevant code below:

header.php

<div class="header">

        <div class="wrapper">

            <h1 class="branding-title"><a href="./">Personal Media Library</a></h1>

            <ul class="nav">
                <li class="books  <?php if($_section == "books")   { echo " on";} ?>"><a href="catalog.php?cat=books">Books</a></li>
                <li class="movies <?php if($_section == "movies")  { echo " on";} ?>"><a href="catalog.php?cat=movies">Movies</a></li>
                <li class="music  <?php if($_section == "music")   { echo " on";} ?>"><a href="catalog.php?cat=music">Music</a></li>
                <li class="suggest <?php if($_section == "suggest"){ echo " on";} ?>"><a href="suggest.php">Suggest</a></li>
            </ul>

        </div>

Catalog.php

<?php 
$pageTitle = "Full Catalog";
$section = null;
if (isset($_GET["cat"])){
    if ($_GET["cat"] == 'books'){
        $pageTitle = 'Books';
        $section = "books";
    }else if($_GET["cat"] == 'movies'){
        $pageTitle = 'Movies';
        $section = "movies";
    }
    else if($_GET["cat"] == 'music'){
        $pageTitle = 'Music';
        $section = "music";
    }
}
include("inc/header.php"); ?>

index.php

     <?php 
        $pageTitle = "Personal Media Library";
        $section = null;
        include("inc/header.php"); ?>

Also, i have changed the conf settings in ini file, but no error seems to be displayed. Please help!. Thanks.

Regards

2 Answers

Chung-Jun Wang
Chung-Jun Wang
3,577 Points

You use $_section in your condition, it should be $section, they are not the same for PHP.

Chung-Jun Wang
Chung-Jun Wang
3,577 Points

Another thing is, in your code it seems add double space between two class, this sometimes make html miss the second class

<li class="books <?php if($section == "books") { echo " on";} ?>"> when it's true this will become <li class="books on"> there are two spaces between books and on because you start PHP after a space then also add the second space when echo.

Carefully use PHP echo html code, spaces matter, so try use <li class="books<?php if($section == "books") { echo " on";} ?>"><a href="catalog.php?cat=books">Books</a></li>

Ammu Nair
Ammu Nair
9,100 Points

Yes, i happened to overlook typing $section as $_section. Thank you so much for the help! Really appreciate it.