View Full Version : Need help building a variable path

11-22-2005, 07:18 PM
I want to use the same PHP include file for my navbar on all pages. The navbar will be displayed at different levels of the directory structure. Additionally I have two languages.

Can anybody tell me how to build a variable path to a linked page.

For example

mysite.com/index.php can be accessed from mysite.com/links.php and also from mysite.com/level2/page.php


mysite.com/_spanish/index.php can be accessed from mysite.com/_spanish/links.php and also from mysite.com/_spanish/level2/page.php

Any links in /_spanish/ will always point to other files in /_spanish/ and links in the English version will always point to other files in the English version

11-22-2005, 08:15 PM
You can just use an absolute path in the href.

11-22-2005, 08:43 PM
I can't figure out how to do this because the absolute path will vary depending on where the surfer is when s/he clicks. I've got two copies of each page, one in English the other in Spanish and both use the same navbar.

So the English index.php is in public_html/ but the Spanish index.php is in public_html/_spanish/

If the user is in /level1/page.php and clicks on index.php link I need to go up one level but if the user is in links.php and clicks on index.php the level stays the same.

Basically I need to figure out how deep down the directory tree the user is and therefore how many levels to go up.

Another problem I had with absolute path was that when I uploaded, the full server path is being included (i.e. levels above public_html) which caused permission problems.

11-22-2005, 09:13 PM
Ah, I see. My initial reaction here is to say that if the sites are the same, but in different languages, you probably should have built your entire structure differently, with only a GET variable's difference. Something like:
if($_GET['lang'] == "es") { require("../_spanish/" . $PHP_SELF); die(); }
If you'd rather keep your (apparent) directory structure, you can just check for the presence of "_spanish" in the path, and tack on another ../. E.G.:
<a href="<?php echo(strstr($SCRIPT_FILENAME, "_spanish") ? "../" . $href : $href); ?>">

11-22-2005, 09:54 PM
Thanks, Twey

I did the site in HTML originally using a wysiwyg package which had built in support for more than one language. It did this by automatically generating page copies in the second language. I've inherited the structure from then.

I've gone over to PHP because of the limitations, bu tI've kept the same structure for the time being while I learn. Once I get the nav working from single-copy includes, I'll start on the pages.

I am already using conditional checks for _spanish in the filename. It did not occur to me to take an absolute path and tag on _spanish if necessary (duhh)

I was also using $_SERVER['DOCUMENT_ROOT'] instead of $PHP_SELF.

Thanks again