Forum Moderators: coopster

Message Too Old, No Replies

Use PHP to swap CSS class

Can it be done and if so, how?

         

MatthewHSE

10:57 pm on Nov 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From a complete PHP newbie, but willing to learn:

I want to use PHP to change a CSS class. For example, I'd like to change this:

<div class="main">

to this:

<div class="notmain">

I want to accomplish this by embedding PHP in my CSS file [webmasterworld.com]. Here's the motivation:

I'm using some .cgi scripts that print HTML. Some CSS classes are hard-coded into the scripts. But, the CSS classes aren't what I need them to be. So I want to use PHP in my CSS file to change the classes.

Can PHP do this, and if so, how can I implement it?

Thanks,

Matthew

jatar_k

11:54 pm on Nov 13, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



of course you can.

So to be able to switch between classes you will need something to test for.

- script name
- located in a certain directory
- the existence of a variable
- a variable with a certain setting
etc..

then you can do something like

if ($testvar == "cgi-files") {
// put some css here
} else {
// put other css here
}

and you will have to make the .css extension parsed by php as mentioned in the thread you referenced.

ergophobe

11:57 pm on Nov 13, 2003 (gmt 0)

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



I think you're talking about two different things.

You can have php decide which class to use or you can have php define how to define the class and always use the same class. The latter method is detailed in the post you cite. The former could be done like so.

if ($x) {
$css_class = "main";
} else {
$css_class = "notmain";
}

echo "<div class=\"$css_class\">";

Tom

jatar_k

12:16 am on Nov 14, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I looked at this
So I want to use PHP in my CSS file to change the classes

and figured you wanted to handle the definition of the class in the css file.

ergophobe

5:41 am on Nov 15, 2003 (gmt 0)

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



Sorry, I didn't mean that Matt and Jatar were talking about two different things. JK's post actually came up while I was writing mine so I didn't see it until I posted and the thread refreshed.

I meant that Matt was talking about two different things.
1. He wants to change the class name in the div and change the CSS that way (i.e. use a different class altogether)
2. He wants to change his CSS (i.e. change the class definition).

The referenced thread and JK answered how to do #1, I was addressing #2.

I thought about amending my post after seeing JK's, but didn't. Sorry for the confusion.

Tom