954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Dynamic Includes

So I'm trying to create one central file for my navigation which is used across my site; however, some of the pages are in different directories. So, for example , let's say I have the following directories --

domain.com/
domain.com/dir1/
domain.com/dir2/

If I wanted to use the same include between all three of these options, is there a way for it to figure out how many "../" need to be included based on where it is?

jrotunda85
Junior Poster
144 posts since Feb 2011
Reputation Points: 19
Solved Threads: 7
 
<? define('SITE_ROOT_PATH',$_SERVER['DOCUMENT_ROOT'].'testproject/')
include(SITE_ROOT_PATH.'config.php');
?>

You can add above code in any file of any directory of testproject.

vibhaJ
Posting Shark
931 posts since Apr 2010
Reputation Points: 161
Solved Threads: 183
 
<? define('SITE_ROOT_PATH',$_SERVER['DOCUMENT_ROOT'].'testproject/')
include(SITE_ROOT_PATH.'config.php');
?>

You can add above code in any file of any directory of testproject.

vibhadevit, thanks for the response. This code; however, won't work for what I'm trying to do since it's going to the root of whatever directory its currently in. Let me give you an example that may be a little better than the earlier one. I have the following files:

index.php
newdirectory/index.php
include/navigation.php

So I want to be able to use the navigation.php onboth the index.php in the root as well as the one in 'newdirectory' So you're code works for the former but not for the latter since it's looking for the navigation file in 'newdirectory' as opposed to the root of the site. Does that make sense?

jrotunda85
Junior Poster
144 posts since Feb 2011
Reputation Points: 19
Solved Threads: 7
 

Can't you just use:

include '/include/navigation.php';
pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Can't you just use:

include '/include/navigation.php';

That is what I'm doing however there are a few files which have includes within another include. So it's causing an issue linking back to the correct place. I've found the answer though, instead of using relative paths, I'm going to use absolute paths (e.g. /home/user/public_html/etc) this is working perfectly! Thanks everyone for the help :)

jrotunda85
Junior Poster
144 posts since Feb 2011
Reputation Points: 19
Solved Threads: 7
 
That is what I'm doing however there are a few files which have includes within another include. So it's causing an issue linking back to the correct place. I've found the answer though, instead of using relative paths, I'm going to use absolute paths (e.g. /home/user/public_html/etc) this is working perfectly! Thanks everyone for the help :)

Actually here's another sort of related question, so using the absolute paths is great for the includes but they don't work obviously in a standard link. So, going on my previous example, let's say I was on index.php (in the root) and click on page 2. That takes me /page2/index.php. Now when I click on home in this directory it takes me back to /page/index.php.

Is there a way to make these nav links where they are always poitining to the correct directory?

jrotunda85
Junior Poster
144 posts since Feb 2011
Reputation Points: 19
Solved Threads: 7
 

In this case Home should point to /index.php and not to index.php

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 
In this case Home should point to /index.php and not to index.php

Right, but the issue is how do you define that when using one central navigation file. So if I say in the navigation include file

Home

Then it will work for part of the site but as soon as you go into another directory it won't go to the correct place.

jrotunda85
Junior Poster
144 posts since Feb 2011
Reputation Points: 19
Solved Threads: 7
 

Not quite sure now what you are trying to achieve.

<a href="/index.php">Home</a>

The above will not solve your problem, because ?

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Not quite sure now what you are trying to achieve.

<a href="/index.php">Home</a>

The above will not solve your problem, because ?

Maybe I am missing something but is that not going to generate errors on the root since it's trying to go into a recursive directory? :-/

jrotunda85
Junior Poster
144 posts since Feb 2011
Reputation Points: 19
Solved Threads: 7
 

As mentioned in the above posts:

<?php
$ROOT = $_SERVER['DOCUMENT_ROOT'];
//OR define into constant ROOT - however, should a constant be a variable??
//constants have the advantage of being global.
define('ROOT',$_SERVER['DOCUMENT_ROOT']);

//so includes and requires use the $ROOT or ROOT prefix
?>


As pritaeas says, making html links and references absolute ('/.....'), should solve your 'many locations' problem.

My 2p.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

Thank you all for your help, I went ahead and tested out pritaeas's code and it fixed the issue. I didn't realize that using '../' or '/' will only recurse into the root regardless of how many you use. Sorry for the confusion :$

jrotunda85
Junior Poster
144 posts since Feb 2011
Reputation Points: 19
Solved Threads: 7
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: