Page 1 of 1

If *url exists*, then show this code...

Posted: Wed May 12, 2010 10:33 am
by kgrote
Hi there,

My problem is quite simple, but I can't figure out how to word it into a simple search term, so I am asking here.

I want to use a php include for a website. The include contains the navigation for the site, which follows this format:

<ul id="nav">
<li><a href="home.php" class="active">Home</a></li>
<li><a href="about.php">About</a></li>
<li><a href="contact.php">Contact</a></li>
</ul>

What I'm trying to do is define the active state of the particular navigation item for each page. So in pseudo-code speak, I want this to happen:

If the URL is equal to "www.mysite.com/about.php", then show "class="active" in the link for the "About" navigation item.

I know how to do this using ExpressionEngine, by writing:
{if segment_1 == "about.php"} class="active" {/if}

So how do I do it using straight PHP? Thanks for the help! Snarky comments aren't needed, so don't bother plzthx.

Re: If *url exists*, then show this code...

Posted: Wed May 12, 2010 11:23 am
by AbraCadaver
Something like:

Code: Select all

if(basename($_SERVER['PHP_SELF']) == 'about.php') echo 'class="active"';

Re: If *url exists*, then show this code...

Posted: Wed May 12, 2010 11:23 am
by ColonelSandersLite
I think you're asking how to get the current script's filename right?

This information can be found with the $_SERVER superglobal.

Since the documentation on what's readily available in the superglobals kinda sucks just put this in a php file to see everything ;)

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

<?PHP
echo "\n<pre>";
echo "\n\$GLOBALS\n";
print_r($GLOBALS);
echo "\n\$_SERVER\n";
print_r($_SERVER);
echo "\n\$_GET\n";
print_r($_GET);
echo "\n\$_POST\n";
print_r($_POST);
echo "\n\$_FILES\n";
print_r($_FILES);
echo "\n\$_REQUEST\n";
print_r($_REQUEST);
echo "\n\$_SESSION\n";
print_r($_SESSION);
echo "\n\$_ENV\n";
print_r($_ENV);
echo "\n\$_COOKIE\n";
print_r($_COOKIE);
echo "\n\$php_errormsg\n";
print_r($php_errormsg);
echo "\n\$HTTP_RAW_POST_DATA\n";
print_r($HTTP_RAW_POST_DATA);
echo "\n\$http_response_header\n";
print_r($http_response_header);
echo "\n\$argc\n";
print_r($argc);
echo "\n\$argv\n";
print_r($argv);
echo "\n</pre>";
?>
		
</body>
</html>


Re: If *url exists*, then show this code...

Posted: Wed May 12, 2010 2:38 pm
by kgrote
AbraCadaver: that did it, thanks!