dynamic CSS classes

HTML, CSS and anything else that deals with client side capabilities.

Moderator: General Moderators

Post Reply
NuttyMonk
Forum Newbie
Posts: 4
Joined: Sat Oct 30, 2010 7:55 am

dynamic CSS classes

Post by NuttyMonk »

Hi all,

i'm using a php file as an include to deal with my menu system so any changes i make in the menu file are reflected in all of the menus on my site.

the problem i have is that i want to dynamically change the class of the html <a> hyperlink tag so that the tag looks different if that page is showing.

i have figure out how to get the name fo the current file. now all i need to do is compare that name with the href part of the <a> hyperlink tag to see whether that tag should have one style of css applied to it or another, depending on whether that tags href refers to the current page.

i can't seem to get my head round this problem which seems simple but actually has me scratchign my head a bit.

can anyone give me some advice on how to do the comparison of the href part of the hyperlink tag with the name of the current page?

cheers
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: dynamic CSS classes

Post by greyhoundcode »

Can we see your existing code?
NuttyMonk
Forum Newbie
Posts: 4
Joined: Sat Oct 30, 2010 7:55 am

Re: dynamic CSS classes

Post by NuttyMonk »

i've actually come up with a solution. it isn't as dynamic as i'd like but it works...

Code: Select all

<?php

$currentFile = $_SERVER["SCRIPT_NAME"];
$parts = Explode('/', $currentFile);
$currentFile = $parts[count($parts) - 1];

$selected = "class='selected'";
$normal = "class='white'";

$home = ($currentFile == 'indexTest.php') ? $selected : $normal;
$howTo = ($currentFile == 'howto.php') ? $selected : $normal;
$bookings = (($currentFile == 'bookings.php') || ($currentFile == 'opening_times.php') || ($currentFile == 'xmas_bookings.php') || ($currentFile == 'large_bookings.php') || ($currentFile == 'cancellation_policy.php')) ? $selected : $normal;
$songs = (($currentFile == 'songs.php') || ($currentFile == 'newsongs.php') || ($currentFile == 'specialcats.php') || ($currentFile == 'searchSongs.php') || ($currentFile == 'mia.php')) ? $selected : $normal;
$rooms = ($currentFile == 'rooms.php') ? $selected : $normal;
$findUs = ($currentFile == 'findus.php') ? $selected : $normal;
$news = ($currentFile == 'news.php') ? $selected : $normal;
$events = (($currentFile == 'events.php') || ($currentFile == 'previous.php') || ($currentFile == 'yourspecial.php') || ($currentFile == 'mobile.php')) ? $selected : $normal;
$aboutUs = ($currentFile == 'aboutus.php') ? $selected : $normal;

echo "
    <table width='100%' height='100%' border='0' cellspacing='0' cellpadding='0'>
      <tr valign='middle' height='28px'>
        <td class='naviText' valign='middle' height='28px'><a href='indexTest.php' " . $home . ">HOME</a></td>
        <td class='naviDashes'></td>
        <td class='naviText'><a href='howto.php' " . $howTo . ">HOW TO SUPERCUBE</a></td>
        <td class='naviDashes'></td>
        <td class='naviText'><a href='bookings.php' " . $bookings . ">PRICES AND BOOKINGS</a></td>
        <td class='naviDashes'></td>
        <td class='naviText'><a href='songs.php' " . $songs . ">SONGS</a></td>
        <td class='naviDashes'></td>
        <td class='naviText'><a href='rooms.php' " . $rooms . ">ROOMS</a></td>
        <td class='naviDashes'></td>
        <td class='naviText'><a href='findus.php' " . $findUs . ">FIND US</a></td>
        <td class='naviDashes'></td>
        <td class='naviText'><a href='news.php' " . $news . ">NEWS</a></td>
        <td class='naviDashes'></td>
        <td class='naviText'><a href='events.php' " . $events . ">SPECIAL EVENTS</a></td>
        <td class='naviDashes'></td>
        <td class='naviText'><a href='aboutus.php' " . $aboutUs . ">ABOUT US</a></td>
      </tr>
    </table>";

?>
At least i only have to modify 1 (or possibly 2) php files now so that all of the pages get updated. It beats modifying 23 pages in one go, even for just a small change :)

thanks anyway
Post Reply