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 Simple PHP Application Creating the Menu and Footer Adding Active States to the Navigation

Nadia Brown
Nadia Brown
8,563 Points

What am I doing wrong?

I added the active states to my navigation for both the shirts and contact list items, but the links are not appearing. I declared the $section variable in both shirts.php and contact.php but for some reason the links do not work. I used the element inspector and it said the section variable was undefined. What should I do?

5 Answers

Amy Kang
Amy Kang
17,188 Points

Hi from what I can see it looks like you're not closing your li elements after "?>" Try this and see if it works.

<li class="shirts <?php if($section == "shirts") {echo "on";} ?>"><a href="shirts.php">Shirts</a></li>
                <li class="contact <?php if($section == "contact") {echo "on";} ?>">
                <a href="contact.php">Contact</a></li>
                <li class="cart"><a href="#">Shopping Cart</a></li>

At the end of the php statement inside your li, close it with another ">"

Amy Kang
Amy Kang
17,188 Points

This is what my working code looked like.

<!DOCTYPE html>
<html>
<head>
  <title><?php echo $pageTitle; ?></title>
  <link rel="stylesheet" type="text/css" href="site.css">
</head>
<body>
  <h1>Shirts 4 Mike</h1>

  <ul>
    <li class="home <?php if($section == "home"){ echo "on"; } ?>"><a href="test.php">Home</a></li>
    <li class="shirts <?php if($section == "shirts"){ echo "on"; } ?>"><a href="shirts.php">Shirts</a></li>
    <li class="contact <?php if($section == "contact"){ echo "on"; } ?>"><a href="contact.php">Contact</a></li>
  </ul>
<?php include('products.php'); ?>
<?php
$pageTitle = "Mike's Full Catalog Shirts";
$section = "shirts";
include('header.php'); ?>

<h1>Shirts For Sale</h1>

<ul class="list">
  <?php foreach($products as $product_id => $product) {
    echo get_list_view_html($product_id, $product);
    } ?>
</ul>

<?php include('footer.php'); ?>

Please post your code if this doesn't help.

Nadia Brown
Nadia Brown
8,563 Points

Thank you Amy, my code is exactly the same, but it still does not load correctly. And my links have stopped working. Take a look at my code:

php
<?php 
$pageTitle = "Mike's Full Catalog of Shirts";
$section = "shirts";
include('inc/header.php'); ?>

    <div class="section page">

        <h1>Mike&rsquo;s Full Catalog Of Shirts</h1>

    </div>

<?php include('inc/footer.php'); ?>
php
<?php 
$pageTitle = "Mike's Full Catalog of Shirts";
$section = "shirts";
include('inc/header.php'); ?>

    <div class="section page">

        <h1>Mike&rsquo;s Full Catalog Of Shirts</h1>

    </div>

<?php include('inc/footer.php'); ?>
php
<html>
<head>
    <title><?php echo $pageTitle; ?></title>
    <link rel="stylesheet" href="css/style.css" type="text/css">
    <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Oswald:400,700" type="text/css">
    <link rel="shortcut icon" href="favicon.ico">
</head>
<body>

    <div class="header">

        <div class="wrapper">

            <h1 class="branding-title"><a href="./">Shirts 4 Mike</a></h1>

            <ul class="nav">
                <li class="shirts <?php if($section == "shirts") {echo "on";} ?>"<a href="shirts.php">Shirts</a></li>
                <li class="contact <?php if($section == "contact") {echo "on";} ?>"
                <a href="contact.php">Contact</a></li>
                <li class="cart"><a href="#">Shopping Cart</a></li>
            </ul>

        </div>

    </div>

    <div id="content">
Nadia Brown
Nadia Brown
8,563 Points

Unfortunately my list items are closed, but I think the formatting got messed up when I copied my code. On here it appears they are carried down to the next line. But in the text editor my list items are on the same line. But thank you anyway! :)

Amy Kang
Amy Kang
17,188 Points

I mean to close the opening li tag. It's missing the ">" at the end of it after the php code.

Nadia Brown
Nadia Brown
8,563 Points

Oh! That clears things up! You meant the opening li tag! I have to be more careful! My code works now. Thank you so much!

Amy Kang
Amy Kang
17,188 Points

Glad it works! Happy coding. :)