Reading the current dir from the URL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rubenski
Forum Newbie
Posts: 5
Joined: Sun Oct 03, 2004 2:18 pm

Reading the current dir from the URL

Post 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.
Breckenridge
Forum Commoner
Posts: 62
Joined: Thu Sep 09, 2004 11:10 pm
Location: Breckenridge, Colorado

Post by Breckenridge »

Here is a url that should help you understand $_SERVER[xyz] variables. Hope it helps

http://us2.php.net/reserved.variables
rubenski
Forum Newbie
Posts: 5
Joined: Sun Oct 03, 2004 2:18 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd use a [php_man]switch[/php_man] statement.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

I concur.
rubenski
Forum Newbie
Posts: 5
Joined: Sun Oct 03, 2004 2:18 pm

Post 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]}";
	}
}
rubenski
Forum Newbie
Posts: 5
Joined: Sun Oct 03, 2004 2:18 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

line 4, '47B809' is an illegal identifier. You need to use quoted strings... fer serious.
rubenski
Forum Newbie
Posts: 5
Joined: Sun Oct 03, 2004 2:18 pm

Post 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...
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply