Forum Moderators: coopster

Message Too Old, No Replies

getURL Path

Show image depending on directory we are in

         

Alternative Future

8:08 am on Aug 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi to the forum,

I know nothing about PHP but have a little task to complete while our php developer is on holiday.
Using PHP I want to REQUEST_URI (I think this is the best way forward - please correct me if I am wrong) then use something similar to subString to get the first directory from the URI string i.e. http://www.example.com/THIS_DIRECTORY/ use this value to get our current location and display an image. So if we are in ABOUT directory we would display an image further down in the HTML in relation to our ABOUT PAGE.

Anyone able to help me out with the code?

With thanks in advance,

-Gs

[edited by: dreamcatcher at 8:20 am (utc) on Aug. 31, 2007]
[edit reason] Use example.com, thanks. [/edit]

cmarshall

10:30 am on Aug 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would use CSS for this, as opposed to PHP, but maybe I don't know enough about the structure of the site.

I do this kind of thing all the time. Use PHP to set the class or ID of a containing <body> or <div> element, then use specific CSS to change the appearance of an element within the block.


<head>
<style type="text/css">
div.icon_div { height:32px;width:32px }
body#category_1 div.icon_div {background-image:url(images/image_cat_1)}
body#category_2 div.icon_div {background-image:url(images/image_cat_2)}
</style>
</head>
<body id="category_1"><div class="icon_div"></div></body>

omoutop

12:01 pm on Aug 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$a = $_SERVER['REQUEST_URI']; // this will give you: /THIS-DIRECTORY/
$a = str_replace("/", "", $a); // this will give you: THIS-DIRECTORY

so now you have $a holding the direcotry name

cmarshall

12:12 pm on Aug 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The only issue is that $_SERVER['REQUEST_URI'] could have a number of levels, and you don't want to just yank out all the slashes, otherwise this might happen:

"

/wp-content/uploads/cat1/
" would become "
wp-contentuploadscat1
".

I'd suggest using the method I outlined, along with some kind of templating mechanism to set the container class.

This is very powerful and flexible.

cmarshall

12:19 pm on Aug 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



'REQUEST_URI' can have a lot of different data. Here's something that I do for a WordPress plugin I wrote:

if ( $_GET['p'] )
{
$class = "xx_post_results";
$page_id = 'post_'.$_GET['p'];
}
elseif ( $_GET['page_id'] )
{
$class = "xx_page_results";
$page_id = 'page_'.$_GET['page_id'];
}
echo "<div id=\"$page_id\" class=\"$class\">";

Then use the method I outlined to display customized content.

omoutop

12:32 pm on Aug 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



you are correct about multiple sub-directories - i didnt thought of that.

I still consider pure php better approach - why mess css php when we can have what we want in 3 lines?

$a = explode("/", $_SERVER['REQUEST_URI']);
$b = count($a);
$dir_i_seek = $a[$b-2];

explaining the -2 part
because REQUEST_URI starts and ends with /, when you explode it you will get an array similar to:
{empty}
{dir1}
{dir2}
{last_dir} // this we seek
{empty}

this array has (n+2) elements, where n the number of our directories - first and last element are empty. In our example the count is 5.
Array elements begin couting from position 0.
Last element is (empty)
Thus we have the $dir_i_seek = $a[$b-2] part

cmarshall

1:53 pm on Aug 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Absolutely right. You don't use CSS to save lines of code. CSS debugging is also a ryal pain.

However, it does give you some flexibility. You can make some pretty massive changes without having to touch PHP code.