Forum Moderators: coopster

Message Too Old, No Replies

PHP to modify page CSS

Add CSS attributes to show/hide parts of page

         

juhlster1021

12:58 am on Mar 26, 2005 (gmt 0)

10+ Year Member



Long-time listener, first-time caller.... I'm just dabbling in php so bear with me.

I'm thinking of setting a variable (let's say $page_id) at the top of each page to help me strategically add css attributes at various points later on. I guess the idea would be to reuse a lot of the same code, page after page, but have each page display uniquely based on its $page_id.

===

Example: to show/hide/style a menu, I could put in the head:

<?php
$pg_id = "m6s2"; // ID this as 2nd subpage, 6th menu group
$m6 =" selected"; // some css attributes that I
$m6s = "show"; // can drop in later
$m6s2 =" youarehere";
?>

And in the body:

<!--there would be similar blocks 1-5 before this -->

<a id="m6" class="menu<?=$m6?>"
href="/yo/yo.htm">Yo Info</a>

<div id="m6s" class="<?=$m6s;?>">

<a id="m6s1" class="sub<?=$m6s1;?>"
href="/yo/yo1.htm">First Subpage of Yo</a>

<a id="m6s2" class="sub<?=$m6s2;?>"
href="/yo/yo2.htm">2nd Subpage of Yo</a>

</div>

<!-- etc. -->

===

Now menu item six will gain a CSS attribute " selected" (to highlight it by color, bold, whatever); the div holding the submenu items will get the attribute "show" (override a "display:none" ); and submenu item 2 link will gain " youarehere" (say, an arrow in the background).

Questions:

1) Does this seem like a reasonable approach? Is there an infinitely more elegant way to do the same thing? Should I be doing this in javascript instead? Id' much rather do it server-side...

2) If I initialize a variable like $pg_id = "m6s2", can I then create the next 3 variables on-the-fly from the charcters in my existing variable -- i.e., for this $page_id, create a variable called $m6 (first 2 characters in $page_id's value) and assign it the value " selected"? Or would an array somehow be of use to accomplish this?

3) if I id the <a> as in <a id="m6" class="menu<?=$m6?>" ... > can I read the tag's id and drop it into the <?=$m6?> snippet automatically? I know I can get the id via javascript...

Thanks for your thoughts and suggestions.

jatar_k

2:25 am on Mar 26, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld juhlster1021,

1) Yes it seems reasonable I have some reskinable sections to a site and all I have to do is drop in a new page with the appropriate id and it sets a bunch of styles as you demonstrated. Works like a charm.

I also define the defaults right in the template to make sure no one loses their style

2) You can pretty much do whatever you want, mine has 12 or 15 values I think, I jat throw them in there as is

3) hmm, I am not totally sure I get you on this one but it gives me a thought that this could be a little more complex than you may need it to be but again, I am not totally sure i get this one

juhlster1021

2:56 am on Mar 26, 2005 (gmt 0)

10+ Year Member



I appreciate the response!

I am just getting the "big picture" about how very nice it can be to augment the ol' html with php etc. I'd love to get a thread started about how to think about setting up skins etc. to let newbies of my ilk get a glimpse of best practices for such. As is, I keep wondering how the really advanced thinkers go about it.

jatar_k

4:23 am on Mar 26, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



maybe try this thread
A dynamic site in 2 minutes [webmasterworld.com]

there are some other good ones in the library too

another one that might interest you
PHP inside CSS [webmasterworld.com]
which also points to this thread
Server Side Scripting in CSS Files [webmasterworld.com]

juhlster1021

6:32 am on Mar 26, 2005 (gmt 0)

10+ Year Member


I like your ...

$content = "pathtocontentpage.html";
include "template.php";

... in that first thread! Man, that's pretty elegant.

I am at that point where I can see so many possibilites, but all so blurry to me, still. What a job it wil be to get it all straight.No site can match webmasterworld for thowing a million fascinating ideas out for people to think about.

A big thanks to you and many others who put your ideas out there day after day -- I have read quite a few of your posts and can only guess at what kind of magic you are keeping back. ;-)

jatar_k

11:46 pm on Mar 26, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> Man, that's pretty elegant

hehe, very true, but..

what works is often the best, I am all about the KISS method, being fancy often gets you in more trouble than it's worth and just delays a project.

I am a firm believer in Pragmatic PHP, if it isn't good for my users then it has no place in my application. :)