Okay I've been trying to figure this out for a while now. I have a website I'm working on and here is basically my structure:
index.php
page-x/
index.php
page-x-y/
index.php
include/
functions.php
css/
main.css
Except add on more sub-folders and index pages. Basically pages at three different levels.
They all need to have css/main.css, but the path from any one index page is going to be different.
Right now I use functions.php to add the CSS, and already have to manually enter the relative path from an index page to functions.php (ie require_once("../include/functions.php"); or ../../include etc
How can I make it so that functions.php can figure out the relative path from any index page it is included on, to css/main.css?
I.E. how would I define $relDir for the following code in functions.php:
echo '<link rel="stylesheet" type="text/css" href="' . $relDir . 'css/main.css" />';
I could manually pass "" or "../" or "../../" every-time to the function, but can I avoid that?
EDIT 3
If I define $BASE = dirname(__DIR__); in functions.php, I can find the root of the site and go from there to get the absolute path to the CSS.
However the css doesn't seem to link properly, on my local machine the resulting link looks like: (I can manipulate the slashes to be consistent but it doesn't help)
"C:\Documents and Settings\Lucas\site/css/main.css"
(Note: I am using an alias on easyPHP so that I can connect to this address from http://127.0.0.1:8888/site/ but__DIR__ finds the ACTUAL directory of the file.
And on the (My universities) server I am testing on comes back with
"/Volumes/Web/Students/MYNAME/Sites/MYSITE/css/main.css"
The ACTUAL URL of the website looks like:
http://www.SCHOOL.com/MYNAME/MYSITE/
The CSS wont link in either situation
EDIT 2 I can define $relDir as:
str_repeat('../', substr_count($_SERVER['REQUEST_URI'], '/');
returns one of the following depending on how deep the index is:
""
"../"
"../../"
This a lot of processing apparently though. It sounds like the best thing to do would be to look into proper testing environments, and use an absolute path. I'll look into that. I'm still a bit of a newb with some of that stuff.
EDIT 1: Okay most people are suggesting I stick with just doing it manually, originally had a function call something like this:
<?php require_once("../include/functions.php");
buildHead("Music", "../"); //($title, $relDir) ?>
I just have to change the ../ to whatever in both places for every page.