PHP Style Changer Experiment
I wanted to open up this latest experiment for comments since it was the comments to Roger Johansson’s Build your own PHP style sheet switcher article that inspired me to add cookie acceptance detection functionality to my own PHP Style Changer Experiment. It seems to work nicely but I figure it’s always good to get some real-world feedback. And since I don’t accept comments at MikeCherim.com I figured I’d do it here. The link above leads to the supporting article, but here’s the actual experiment page, and here’s the well-commented script provided as a text file. Feel free to use it and tell others if you like it — it is safe from XSS so use it with confidence. It’s been in use a long time but I never released it.
For this experiment I’d like to say thanks to “ClamCrusher” at the SitePoint Forums who helped me unloop a loop that was driving me crazy. I also owe a nod to PHP.net without whom I’d be dead in the water. I think Jona should also get a thank you for help with the original script over a year ago. He’s helped me so many times in the past I don’t really remember if he helped me with this or not, but I’d rather err on the side of caution and give thanks if thanks are indeed due. I’m just a PHP novice, but the language blows me away and I will continue to learn it. It’s amazing.
If you’ve never seen my experiments before, check it out as I have a number of them. Some are cool. Of the better ones I offer my CSS: Smart Corners, PHP: Font Sizer, CSS: Map Pop (combination CSS Image Map and CSS Popups), and many more; some refined, some quite experimental.
Mike Cherim responds:
Posted: September 2nd, 2006 at 11:05 am →
Cookie Negotiation Script Block
I decided to break out the portion of the script (without most of the comments) — the part which handles cookie acceptance negotiation — because it can really be quite useful for any script that relies on setting a cookie. If cookies aren’t allowed by the user, then using this script the developer can remove the feature or offer a usable alternative:
To use this be sure to place it at the very top of the page, before the DTD, any whitespace, and even before setting the other script’s cookie.
Note: “CN” stands for “Cookie Negotiation” in the script block above. This was “PSC” for “PHP Style Changer” in the original style changer script.
Georg responds:
Posted: September 3rd, 2006 at 10:43 am →
I’ll take a php stylesheet switcher over a javascript one any day - if I only want to switch between stylesheets.
Now, since I rarely ever see the need for just switching between two or more stylesheets, it looks like I’m still stuck with the old javascript stylesheet swapper.
You shouldn’t happen to know about a php solution that’ll let me combine and/or add/subtract any of a dozen or so stylesheets in any order at any time, do you..?
Mike Cherim responds:
Posted: September 3rd, 2006 at 12:08 pm →
Assuming I’m understanding what you’re asking, Georg — I’m not sure I do — I suppose you could use this is a number of different ways. You could have a master style sheet with a basic layout then offer a number of small sheets to offer different individual styles, or you could use this to serve several style sheets at the same time so long as the individual sheets didn’t override one another by offering conflicting styles (two styles for the same element, id, or class), and this could be done incrementally. Example: Basic layout > Click to make text larger > Click to change color > Click to do both, so on and so forth. Each user input offering any number of varied configurations. In the script I do offer comments on how to offer more than two sheets/choices, but you just have to make sure you change the interface so as to not provide a single link but rather a list of links.
You could also add a list of links that offer very specific conditions returned to the head as defined styles on any particular element, id, or class. This, I suppose, is best described on the PHP: Font Sizer I made where you’re actually specifying a value to some element, id, or class.
Also, this could be done automatically in a given sequence by using header Location redirects (serve one sheet, wait, serve another, wait, serve another, wait, etc.). This latter thing is an idea I’ve long had to actually create a sequential tutorial on how to build CSS-styled a website, one step at a time. The user would go to a single page and the site would be built before their eyes. They’d have to do nothing but watch. I haven’t done this yet as it’d be pretty involved and time-consuming on my part.
But, all this said, I don’t know if I’m answering your specific question properly so do feel free to restate it if I’m off-base. I’m sure it can be done, though, whatever if may. PHP is quite powerful and flexible.
Georg responds:
Posted: September 3rd, 2006 at 1:55 pm →
Mike, yes I know php is powerful, but since my ability to do anything useful with php is virtually non-existent, I simply have to ask around about what it can do before spending time on learning php in depth. I’ll probably spend as much time on php as I have spent on CSS so far, so it would be a pity to find out after a while that it isn’t powerful enough - for me.
As an example: on an old page - http://www.gunlaug.no/homesite/main_7_rtg_4.html - created 2 years ago, I played around with a javascript stylesheet swapper. One regular stylesheet combiner for the basic section-wide variations, and an additional one for layout-variations on that page.
A total of over 20 stylesheets combined in 6 different settings - 3 of which are memorized in a cookie and used across that site-section. All handled by one script.
I know some of what I do on that page can be achieved better in php without involving swapping of stylesheets, but I’d like to have a php stylesheet switcher/swapper that is that powerful. Any problems with that that I should know about before throwing myself into mastering php?
Mike Cherim responds:
Posted: September 3rd, 2006 at 4:40 pm →
I get you now I think. In your case you’d create a master CSS to handle 99% of the styles, then you could use either of the examples I showed — the PHP Font Sizer type or the PHP Style Changer type. With the former you’d allow the user to modify specific elements, such as a font size selection on the main content area for example. With the latter, you’d allow the user to call a specific sheet with a certain combination of set styles, such as a large font size with a left column, or a small size with a right column, so on and so forth.
I feel the latter would be more work for you, though, and I’d recommend the PHP Font Sizer type of script. I think it would be more efficient. Each one would use a separate cookie so that each variation could be set and recalled independently and the number of variations would be limitless. But really you could successfully do what you want to do with either method.
The short answer, however, is yes. You could absolutely do this with PHP, there is no doubt about it. The advantage is the functionality would be available to all users that allow cookies (use the cookie part of the PHP Style Changer script with the Font Sizer script) because JavaScript wouldn’t be required. Also, arguably, I think PHP is more efficient. The disadvantage would be the selections wouldn’t be available by way of the browser — View > Page Style (this requires JavaScript — so you would need to create a user interface as you have on your site right now, using inputs to GET styling properties.
It is possible to offer it both ways, using JavaScript first, then backing that up with PHP, but I don’t really know how to do that (I know more about PHP than I do JavaScript). Should you learn PHP? Yes! I’d say it would be quite beneficial to you to do so and not at all a waste of energy. If for nothing else, having any function available regardless of the user agent is extremely valuable. Most people have JavaScript enabled, but still it’s a good feeling that the possibility they don’t is no longer of concern.
Georg responds:
Posted: September 3rd, 2006 at 6:22 pm →
Ok Mike, I’ll put “mastering php” on my schedule, and see if I can make something useful out of it - one day. Might be fun.
Thanks.
Mike Cherim responds:
Posted: September 3rd, 2006 at 7:10 pm →
It should be fun, Georg. I consider myself a PHP novice at best and it drops my jaw with amazement every time I learn a new trick or see a bit of what it can do. I will say, the “include” function alone is an enormous time-saver when creating web sites. It allows me to reuse global files like headers, navigation, and footers which shaves days off of any project. Just as CSS makes managing and editing styles so much easier, PHP allows this with just about every other aspect of a project.
Have a great time!