...

View Full Version : Nested directories and creating a dynamic link to them



thepocketgeek
08-28-2009, 11:07 PM
I am working on creating a very basic cms system. Just doing it to improve my understanding on how PHP works. Here is my problem.
When I create my basic HTML type site, I separate the header, navigation, content and footer areas into includes.

This way, if I change a link in the footer or header, I only have to change one file not ten.

The problem I am running into is when I want to call those includes from within a sub-directory. If I call those files from the sub directory (i.e. root/products) the links in the header and footer won't work because those links are now looking within the folder where the file has been called.

What would be the best way to solve the problem?

I have thought about creating some sort of variable to place in front of the link:



<?php
if(some php voodoo to determine if the file is in a subfolder or not...){
$directory = "../";
}else{
$directory = "";
}
?>

<a href="<?php echo $directory; ?>/web_link.php">Web Link</a>


Any ideas on the best way to work this problem?

abduraooft
08-29-2009, 07:54 AM
I'd use absolute paths like

<a href="/file1.php">file inside root</a>
<a href="/dir1/file1.php">file inside dir1</a>
<a href="/dir1/dir2/file1.php">file inside dir1/dir2</a> Check http://www.codingforums.com/showpost.php?p=784167&postcount=9

thepocketgeek
08-31-2009, 03:02 PM
The only problem with using the absolute paths is that I would have to create a second file. One for the root and one for a sub directory.

I am looking at how CMS systems work and they have managed to get links to work inside of directories. I guess I am trying to replicate that functionality without having to install a full blown CMS system.

abduraooft
09-01-2009, 07:24 AM
The only problem with using the absolute paths is that I would have to create a second file. One for the root and one for a sub directory. Not really! How did you conclude like that?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum