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

Alex Tasioulis
Alex Tasioulis
4,950 Points

glitch with very basic php


            <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>

I used this stuff (just following along with Randy's lesson on very simple PHP) and it's doing what it's supposed to:

When I'm on the shirts page, it's underlined like this:


On the contacts page, it's again nicely underlined like this:


But on the index page, this happened:


I have no idea why this is happening. Any thoughts?

Alex Tasioulis
Alex Tasioulis
4,950 Points

additionally, this problem is solved when i add "isset($section)" but why does it appear in the firstplace? do I get a glitch in php everytime i'm asking if ($var == "anything") and the var is not set?

4 Answers

Well actually in the end it will be much simpler. As you create a page if you have the section title, he teaches you separation of concerns later on for the Model, View, Controller method. Each page when you have a controller typically has some sort of title function of location in the top of the file to let the controller know what to append to the title. This is especially true if you are using codeigniter.


    class Pages extends CI_Controller {
        public function view($page = 'home'){
                if (! file_exists('application/views/pages/'.$page.'.php.')){
                $data['title'] = ucfirst($page); //This actually passes the title to the header

                $this->load->view('templates/header',$data); //Here

Its a rough example. With isset() you would still need to set a $section for each page you intended to underline to make it set. You could use empty() as well. In the end though I think Randy wanted to prep the site to demonstrate his MVC work later on.

Alex Tasioulis
Alex Tasioulis
4,950 Points

Oh I see! Well, that's exciting. I'll keep trucking on. :D Thank you for your very helpful responses.

Since this only happens on the index.php page it could be something in the index file itself, but I suspect its something in his CSS file. Have you tried downloading a later version maybe next phase and comparing?

Your index section should be:

$section = "home";

I know that for sure as it has no underline.

I haven't been far enough into the project yet to know if he added $section = "home" because it was actually needed on the page or only to stop the error because he noticed it later on and they didn't want to go back and re-shoot the video. That's understandable.

This has come up a few times recently in the forums and I feel that they should at least update the project files and perhaps put a note for this video on how to fix the code. Point out that the video is not showing isset() but you should put it in your code.

If you're interested in the details about why you saw what you did then I posted a more detailed answer here:

Alex Tasioulis
Alex Tasioulis
4,950 Points

Thank you, Jason, and thanks for the link: that clears it all up.

You're welcome. Glad it helped.

Alex Tasioulis
Alex Tasioulis
4,950 Points

Thank you. I downloaded a later project file and yes, that's what he's done, added section = home... I didn't want to do that. I find that defeats the point of it all. We wanted to be dynamically handling these things and avoid having to make changes on each page individually... and yet now instead of adding "on" in pages, we have to add "$section = blabla" to each page... that's not even shorter. That's disappointing!

I like isset better anyways...