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

PHP help. Little more advance task I'm stuck on.

Its a little lengthy but please bare with me. I need help. I'm building a site based on the php badge in treehouse. I am using the technique of including the header and nav in its own separate file like I was taught in the video.

In my nav I have a drop down for beaches and the list of beach names are in the drop down. I want to organize them in my directory a certain way.

I gave each beach it's own page. I want to put all these pages in a folder named beaches in my directory. The problem is when I do this. The links are not updated to go into the folder and the page ends up blank. How can I write the php to correct this.

Please help and thanks so much

7 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

That's a good question. I've started recording the next set of PHP tutorials, and I am covering that exact thing! :~) It'll be a little while before the content is released, but here's a short answer. You'll want to reference the include files by an absolute server path. Instead of this --

inc/header.php

-- it'll need to look more like this --

/home/username/htdocs/inc/header.php

Does that help you

yes it does randy. thanks but what about the path when it is on an actual server. what would that look like?

thanks again for answering randy. love your videos

dan

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Actually, it will look similar on the server. It will probably be something different. The $_SERVER array should have the value that you need. Try doing this at the top of the header.php file:

<?php

    echo $_SERVER["DOCUMENT_ROOT"];
    exit;

?>

That should display the server path to the htdocs directory.

Before you had answered my question. I trouble shooted myself and I found a solution and I want to run it by you to make sure it is not bad practice. All I did to make it work was add ../ to the beginning of all the paths and it worked and the page rendered correctly. Is this an ok solution?

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Yes, that works just fine. This is known as a "relative server path" because the path is relative to the location of the current file. I'll talk about this more in an upcoming tutorial, about using a configuration file and a constant. But I would use a relative server path, just like you've done, to include that configuration file. Stay tuned for that! :~)

Thanks so much randy. thanks so much for the help man. love your tutorials