Page 1 of 1

Reading the current dir from the URL

Posted: Sun Oct 03, 2004 2:24 pm
by rubenski
Hi everyone,

I am creating a site and I want to use the current directory as a variable for setting page color and a bunch of other things.

How would I extract dir1 from http://www.mywebsite.nl/dir1/pagename.php?

I have done some reading at php.net and I understand there are two steps to do this.

1. Get the actual URL
2. Read the right piece from that URL

I assume PHP needs to know the basedir of my site on the server before it can extract dir1?

Could you help me do this? I am a bit confused by all the URL and dir functions PHP offers.

Posted: Sun Oct 03, 2004 2:30 pm
by Breckenridge
Here is a url that should help you understand $_SERVER[xyz] variables. Hope it helps

http://us2.php.net/reserved.variables

Posted: Sun Oct 03, 2004 2:45 pm
by rubenski
Thanks Breckenridge. I figured it out pretty quickly.

Code: Select all

<?php
$path = pathinfo($_SERVER['PHP_SELF']);
$directory=$path["dirname"];
echo "$directory";
?>
I will now match $directory against a predefined list of directory names in an if else structure. Do you think this is a good way to go if I want to set the page color, a link menu and some graphics based on the directory name?

Posted: Sun Oct 03, 2004 3:48 pm
by feyd
I'd use a [php_man]switch[/php_man] statement.

Posted: Sun Oct 03, 2004 3:51 pm
by m3mn0n
I concur.

Posted: Sun Oct 03, 2004 8:32 pm
by rubenski
Thanks all,

I've got it figured out. Perhaps others that find this thread will benefit from this piece of code:

(Note I am not a superduper programmer. This code might not be maximum efficient or logical, but it's serves my purposes)

Code: Select all

// get the current directory name

$path = pathinfo($_SERVER['PHP_SELF']);
$directory=$path["dirname"];

// create an array of categories (dirs) and things related to them (top_color)

$multi_array = array(
	
	array(1, cat1, top_yellow),
	array(2, cat2, top_green), 
	array(3, cat3, top_blue),
	array(4, cat4, top_purple), 
	array(5, cat5, top_darkblue), 
	array(6, cat6, top_orange),

	);
// Match every cat from $multi_array against the current dirname 

for ($i = 0; $i <  sizeof($multi_array); $i++)
{
	
	if(ereg($multi_array[$i][1],$directory)) 
	{
		//if they match carry out actions specific to the current directory

		$name_of_top_image = "{$multi_array["$i"][2]}";
	}
}

Posted: Sun Oct 03, 2004 9:18 pm
by rubenski
Strange, I receive a parse error (Parse error: parse error, unexpected T_STRING, expecting ')' in ....) when I try to add hex color codes to the array.

This works:

Code: Select all

$multi_array = array(
	
	array(1, hh, top_geel, F2C213),
	array(2, decanen, top_groen, ggg), 
	array(3, werkgevers, top_geel, ggg),
	array(4, opleidingen, top_geel, ggg), 
	array(5, studeren, top_geel, ggg), 
	array(6, den_haag, top_geel, ggg),

	);
But with a second color added it returns the parse error:

Code: Select all

$multi_array = array(
	
	array(1, hh, top_geel, F2C213),
	array(2, decanen, top_groen, 47B809), 
	array(3, werkgevers, top_geel, ggg),
	array(4, opleidingen, top_geel, ggg), 
	array(5, studeren, top_geel, ggg), 
	array(6, den_haag, top_geel, ggg),

	);
How is that possible?? The parse error points to line 4, the one with 47B809 in it.

Posted: Sun Oct 03, 2004 10:33 pm
by feyd
line 4, '47B809' is an illegal identifier. You need to use quoted strings... fer serious.

Posted: Mon Oct 04, 2004 5:03 am
by rubenski
Quotes work allright. Thanks. I still wonder why I only get a parse error when using 47B809 and not with 'ggg'or another hex code...

Posted: Mon Oct 04, 2004 6:02 am
by twigletmac
Make sure your error_reporting is set to the highest level while you are developing (E_ALL) as you'd probably see quite a few more notices then.

Mac