Forum Moderators: coopster

Message Too Old, No Replies

serverpath if statement

creating an if statement by server path

         

thejabok

5:23 am on Nov 11, 2009 (gmt 0)

10+ Year Member



Hey all,

I'm new to all this, so please forgive me if this is a silly question. I've tried finding some answers before posting, but the available information is so overwhelming that I couldn't make much sense out of it.

I'm trying to create an included navigation that emphasizes the link to the currently loaded page. So far, everything's working fine, I've used the following code on each page to determine the page name

<?php $page = basename($_SERVER['SCRIPT_NAME']); ?>

and the following "if" statement in the <a href> tag in the included header.php to assign the corresponding css class:

<a href="/example.php" <?php if ($page == 'example.php') { ?>class="selected"<?php } ?>>Example</a>

This works fine until I reach a page that has a submenu that links to several pages in subordinate folders. As soon as I visit one of these pages, none of the links in the navigation is highlighted as I can hardly include every single page in these folders in the if statement. I would therefore like to create an addition to the if statement along the lines of

<a href="/example2.php" <?php if ($page == 'example2.php' or $path == 'XYZ') { ?>class="selected"<?php } ?>>Example2</a>

What exactly would I have to specify instead of the XYZ and - of course - in the head of the single pages?

I'd appreciate any help here, and again, I'm sorry if this is a dummy question...

Cheers,
Jakob

TheMadScientist

5:38 am on Nov 11, 2009 (gmt 0)

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



There's quite a few ways to do what you are asking, but the simplest, most straight forward, if not the most elegant and scalable, is probably:

<a href="/example2.php" <?php if ($page == 'example2.php' or strpos($_SERVER['REQUEST_URI'],'/the/path/to/match/')!==FALSE) { ?>class="selected"<?php } ?>>Example2</a>

thejabok

5:53 am on Nov 11, 2009 (gmt 0)

10+ Year Member



Thanks so much, MadScientist! That totally did the trick!

TheMadScientist

6:25 am on Nov 11, 2009 (gmt 0)

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



Glad you got it working, and Welcome to WebmasterWorld!

thejabok

7:14 am on Nov 11, 2009 (gmt 0)

10+ Year Member



Thanks!

[edited by: eelixduppy at 12:47 pm (utc) on Nov. 11, 2009]
[edit reason] no URLs, please [/edit]