Jump to content

[SOLVED] Conditional CSS using PHP?


chimpanzee

Recommended Posts

I don't even know if this is possible or how complicated it would be but I would like to use php to define the class of a div depending on the filename...

 

I have a consistent navigation bar in my website which is inside an include and therefore rendered in the same way on every page. Each button in the nav bar is a div:

<div class="navItem"><a href="index.php">Introduction</a></div>

Is it possible to add some PHP to say that if the current file is "index.php" then class="navItemActive" else class="navItem"? (or something along those lines :))

 

Thanks.

Link to comment
Share on other sites

hi.

i think you could do it like this.

 

if(strrpos($_SERVER["PHP_SELF"], "index.php"))
{
//css for index
}else{
//other css
}

 

Thanks. Would that be used within the div tag like so:

<div if(strrpos($_SERVER["PHP_SELF"], "index.php"))
{
class="navItem"
}else{
class="navItemActive"
}><a href="index.php">Introduction</a></div>

 

Sorry, I'm a newb at PHP  ???

Link to comment
Share on other sites

Hi.

 

Thanks for your help. I tried that and it returned some strange results. The page is at http://www.bareonline.co.uk/site2/index.php . Could you take a look.

 

I've done this for each div (and obviously changed the bits surrounded by !'s accordingly(and obviously not included the !'s in the actual code :))):

<div class="
<?php
if(strrpos($_SERVER["PHP_SELF"], "!index.php!"))
{
  echo 'navItemActive';
}else{
  echo 'navItem';
}
?>
"><a href="!index.php!">!Introduction!</a></div>

Link to comment
Share on other sites

<div class="
<?php
if($_SERVER['PHP_SELF'] == '/index.php'))
{
  echo 'navItem';
}else{
  echo 'navItemActive';
}
?>
"><a href="index.php">Introduction</a></div>

try that..

note the forward slash / before the file name. I believe that would be needed

Link to comment
Share on other sites

Sure:

<table style="width: 100%; padding: 2px; margin:0px">
			<tr>
				<td><div class="
<?php
if($_SERVER['PHP_SELF'] == '/index.php'))
{
  echo 'navItem';
}else{
  echo 'navItemActive';
}
?>
"><a href="index.php">Introduction</a></div></td>
				<td><div class="
<?php
if($_SERVER['PHP_SELF'] == '/about_bare.php'))
{
  echo 'navItem';
}else{
  echo 'navItemActive';
}
?>
"><a href="about_bare.php">About BARE</a></div></td>
				<td><div class="
<?php
if($_SERVER['PHP_SELF'] == '/features.php'))
{
  echo 'navItem';
}else{
  echo 'navItemActive';
}
?>
"><a href="features.php">Features</a></div></td>
				<td><div class="
<?php
if($_SERVER['PHP_SELF'] == '/news.php'))
{
  echo 'navItem';
}else{
  echo 'navItemActive';
}
?>
"><a href="news.php">News</a></div></td>
				<td><div class="
<?php
if($_SERVER['PHP_SELF'] == '/who_are_we.php'))
{
  echo 'navItem';
}else{
  echo 'navItemActive';
}
?>
"><a href="who_are_we.php">Who Are We?</a></div></td>
				<td><div class="navItem"><a href="forum.htm">Forum</a></div></td>
			</tr>
		</table>

Link to comment
Share on other sites

ah, my fault.. I left an extra ) at the end of the if().. try this:

<table style="width: 100%; padding: 2px; margin:0px">
			<tr>
				<td><div class="
<?php
if($_SERVER['PHP_SELF'] == '/index.php')
{
  echo 'navItem';
}else{
  echo 'navItemActive';
}
?>
"><a href="index.php">Introduction</a></div></td>
				<td><div class="
<?php
if($_SERVER['PHP_SELF'] == '/about_bare.php')
{
  echo 'navItem';
}else{
  echo 'navItemActive';
}
?>
"><a href="about_bare.php">About BARE</a></div></td>
				<td><div class="
<?php
if($_SERVER['PHP_SELF'] == '/features.php')
{
  echo 'navItem';
}else{
  echo 'navItemActive';
}
?>
"><a href="features.php">Features</a></div></td>
				<td><div class="
<?php
if($_SERVER['PHP_SELF'] == '/news.php')
{
  echo 'navItem';
}else{
  echo 'navItemActive';
}
?>
"><a href="news.php">News</a></div></td>
				<td><div class="
<?php
if($_SERVER['PHP_SELF'] == '/who_are_we.php')
{
  echo 'navItem';
}else{
  echo 'navItemActive';
}
?>
"><a href="who_are_we.php">Who Are We?</a></div></td>
				<td><div class="navItem"><a href="forum.htm">Forum</a></div></td>
			</tr>
		</table>

Link to comment
Share on other sites

oops.. just realised that the echo statements were the wrong way round..

<table style="width: 100%; padding: 2px; margin:0px">
			<tr>
				<td><div class="
<?php
if($_SERVER['PHP_SELF'] == '/index.php')
{
  echo 'navItemActive';
}else{
  echo 'navItem';
}
?>
"><a href="index.php">Introduction</a></div></td>
				<td><div class="
<?php
if($_SERVER['PHP_SELF'] == '/about_bare.php')
{
   echo 'navItemActive';
}else{
  echo 'navItem';
}
?>
"><a href="about_bare.php">About BARE</a></div></td>
				<td><div class="
<?php
if($_SERVER['PHP_SELF'] == '/features.php')
{
   echo 'navItemActive';
}else{
  echo 'navItem';
}
?>
"><a href="features.php">Features</a></div></td>
				<td><div class="
<?php
if($_SERVER['PHP_SELF'] == '/news.php')
{
   echo 'navItemActive';
}else{
  echo 'navItem';
}
?>
"><a href="news.php">News</a></div></td>
				<td><div class="
<?php
if($_SERVER['PHP_SELF'] == '/who_are_we.php')
{
   echo 'navItemActive';
}else{
  echo 'navItem';
}
?>
"><a href="who_are_we.php">Who Are We?</a></div></td>
				<td><div class="navItem"><a href="forum.htm">Forum</a></div></td>
			</tr>
		</table>

 

if that doesn't work could you echo $_SERVER['PHP_SELF'] and see what it outputs

Link to comment
Share on other sites

Okay, I was looking at your site and realised that you'd tried echoing the $_SERVER['PHP_SELF'] and it actually echoed the text $_SERVER['PHP_SELF'].

This is because you were trying to echo a variable and I presume your code was something like this:

echo "$_SERVER['PHP_SELF']";

when echoing variable you don't use ", so it would be:

echo $_SERVER['PHP_SELF'];

 

Also I think I've realised what the problem is. The code is fine, but you are using it on a sub folder in your directory, so you need to use

if($_SERVER['PHP_SELF'] == '/site2/index.php')

for the testing purposes. I presume you're just testing the files in this directory whilst working on it, then once finished they'll be moved to the root directory replacing the other files?

If so, you'll need to then go back to that part after testing and remove all the /site2 parts.

 

Hope this helps

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.