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

how to structure php includes /paths with folders in website structure

I'm wondering how to structure php files when I have a strcuture like Root which contains > index.php

then I have a folder for other php pages and I am trying to include the header and footer include files, but the problem is the path would be different depending on if the page including the header/footer is in the root directory or its own folder. Basically it maters because in my footer file I have to include script files and they have to be able to point to the correct path, so ... I would sometimes need '../example/example.js' if the file is in a folder or it would just be 'example/example.js if the file is in the root. How do I get around this problem?

for now i just solved this problem by doubling up on the src files like so:

<link rel="stylesheet" href="../stylesheets/app.css" />
<script src="../bower_components/modernizr/modernizr.js"></script>
<link rel="stylesheet" href="stylesheets/app.css" />
<script src="bower_components/modernizr/modernizr.js"></script>

4 Answers

Hi Ryan,

Just spotted your comment. If it is in order to reference the styles, etc. can you try replacing:

<link rel="stylesheet" href="../stylesheets/app.css" />
<script src="../bower_components/modernizr/modernizr.js"></script>
<link rel="stylesheet" href="stylesheets/app.css" />
<script src="bower_components/modernizr/modernizr.js"></script>

with:

<link rel="stylesheet" href="/stylesheets/app.css" />
<script src="/bower_components/modernizr/modernizr.js"></script>

Adding a / at the beginning of the path with reference it according to the root of the site and should save you having to duplicate it.

Thanks

-Rich

Hi Ryan,

I hope I'm understanding this correctly (apologies if not).

Rather than use '../example/example.js' or 'example/example.js' this reference may help:

http://css-tricks.com/php-include-from-root/

This would then be relative to the root of the website in all subfolders too.

Thanks

-Rich

hi Ryan Hellerud ,

in the php course Enhancing a Simple PHP Application - chapter 2 Cleaning URLs with Subfolders - this topic is covered. It explains the differences and usecases for relative paths vs. root-relative and absolute paths. perhaps that might be interesting for you.

hope that helps and have a nice day ;)

Thanks guys! You're all awesome!