Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic generation of links

Status
Not open for further replies.

GWHicks

Programmer
Dec 11, 2002
39
US
I am trying to create my web pages with some dynamic structure and am running into some problems.

Here is the structure I was trying to use:

root directory
files
index.php
directories
navigation
files
navigation.html
toolbtn1.swf
toolbtn2.swf
webhosting
files
index.php

What I am attempting is to have my navigation files stored in the navigation directory and used as an include in each of my pages. The problem I am having is if the page that the include is in is not in the same directory as the navigation page then it doesn't work properly.

I know it has something to do with the link structure being that sometimes I will need to have it link to navigation/navigation.html but if the page is down in a directory below the root it would actually need ../navigation/navigation.html

I hope I have explained this enough to get the problem across to you. If you need further information just let me know and I will see if I can further clarify.

Thanks in advance!
Greg Hicks

Greg Hicks
VB.Net (Newbie) Programmer
 
I don't have a problem getting the include command to work, it is when the file that it gets included in is in a different directory. Basically the way I have it now I will have to have a seperate file to include at each directory structure to replicate the ../ or ../../ for each directory level that it gets included in.
The other alternative would be to hop in and out of php putting the preceding ../ as needed. But that solution seems less than elegant and I am sure there must be a better way as lots of people have it working fine. And having multiple includes defeats the purpose of an included file!
Thanks.
Greg

Greg Hicks
VB.Net (Newbie) Programmer
 
Well, remeber that an include file just straight copies the text. So any file references that you make in your includes are acually starting from the php file that incluedes them. the php_self vars might be of use to you:
$_SERVER['PHP_SELF'] returns the dir and name of the php file that is being executed.
dirname($_SERVER['PHP_SELF']) returns the dir of the php file being executed
-ben
 
I have tried your method and here are the results (assuming I understood you correctly and implemented it as you meant! (-8

Here is my code:
<?php
$path = dirname($_SERVER['DOCUMENT_ROOT']);
include ($path.'/header.html');
include ($path.'/navigation/navigation.html');
?>

That is in this file:
The absolute path to header.html is:

The absolute path to navigation.html is:

Here is what I get back:
Warning: main(/var/ failed to open stream: No such file or directory in /var/ on line 10

Warning: main(): Failed opening '/var/ for inclusion (include_path='.:/php/includes:/usr/share/php') in /var/ on line 10

Warning: main(/var/ failed to open stream: No such file or directory in /var/ on line 11

Warning: main(): Failed opening '/var/ for inclusion (include_path='.:/php/includes:/usr/share/php') in /var/ on line 11

Thanks for any help you can give.
Greg

Greg Hicks
VB.Net (Newbie) Programmer
 
And this code includes the files as it should.

<?php
$path = dirname($_SERVER['DOCUMENT_ROOT']);
include ('../header.html');
include ('../navigation/navigation.html');
?>

However there is an issue with the header.html file having the same problem with the relative path changing based on the file that it is included in. Just one problem generated after another!

Greg

Greg Hicks
VB.Net (Newbie) Programmer
 
Couldn't this whole problem be solved though the use of absolute paths?

In the include statement, use filesystem-absolute pathnames:

include ('/full/filesytem/path/to/navigation.html');

In navigation.html, use absolute URLs:

<a href=" here</a>

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
OK, it is working, but still not as elegant as I think it can be. There must be a function in PHP that returns the url of the site you are at.

What I ended up with is this:
<?php
include ('../header.html');
include ('../navigation/navigation.html');
?>
That is in the myurl/webhosting/index.php file and the navigation and header files I used absolute links in. I just thought there would be a way to tell it to include a file and have it build the structure to that file.
Now that I write that however I realize what I am asking and my approach seems to be fairly logical.
Thanks for all the help.
Greg

Greg Hicks
VB.Net/PHP (Newbie) Programmer
 
Oh, and on another minor note, if I used the path info (i.e. /var/html/ it couldn't find the page. Would I need to precede that line with something to get it to start at the root level of the structure?
Just curious on that. One of the little tidbits I did encounter as using one of the PHP functions would return /var/html/ I could then append whatever I wanted to it for the structure, but it wouldn't give me the desired result.
Greg

Greg Hicks
VB.Net/PHP (Newbie) Programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top