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

Shirts 4 Mike $section

Quick question, what exactly is the $section = "contact"; variable doing in the project? where "contact" is the context of which page you're on.. just curious on the functionality

2 Answers

It's setting the active or highlight class to the menu. I believe in the context of the shirts for mike project, the page you are currently viewing is underlined in the menu bar.

This allows us to have our menu bar only coded in one spot, and than included on every page. This makes it super simple to add or remove new pages to the menu. We can do it in one spot and all the pages will reflect the change. The problem is that for each unique page, how do you have an additional css class that highlights the page you are on in the menu, if every page uses the same menu code. There is where the $section comes in.

In our include header.php file, when you have your list items that create your menu, you might have something that looks like:

<ul class="nav pull-right">
<li class="<?php if ($section == "home"){ echo "menu-active"; } ?>"><a href="<?php echo BASE_URL; ?>">Home</a></li>
<li class="<?php if ($section == "about"){ echo "menu-active"; } ?>"><a href="<?php echo BASE_URL; ?>about/">About Us</a></li>
<li class="<?php if ($section == "portfolio"){ echo "menu-active"; } ?>"><a href="<?php echo BASE_URL; ?>portfolio/">Portfolio</a></li>
<li class="<?php if ($section == "contact"){ echo "menu-active"; } ?>"><a href="<?php echo BASE_URL; ?>contact/">Contact</a></li>
</ul>

Than, on the page we are coding, before doing anything, we'd want to open a PHP block that would look something like:

<?php
$section = "about";
require_once("../inc/config.php");
include(ROOT_PATH . "inc/header.php"); 
?>

Now, when the server processes our PHP and sends it to the browser, it's going to see that on that page, my section variable equals "about", and in the menu, we check see that on if statement is true because if $section is equal to "about" than echo "menu-active", which is conveniently wrapped in the li class tags.

Now I just use CSS to style a menu-active class to have some sort of different appearance, and we have a flexible, and modular menu that more closely sticks to the DRY principle. (Don't Repeat Yourself).

Thanks Kevin, that helped a lot and I already incorporated that into the project I'm working on now.

It's so that you've got a variable you can echo into things like the nav menu to highlight current page.

I think, been a while since I did that part of the course, what does the video say?