Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » php » Style Switcher

Style Switcher

How to write a script that will allow you to easily load another stylesheet.

Here is the code to set the style

<?php
if(!empty( $_GET['theme'] ))
	{
	$style = $_GET['theme'];
	switch( $style )
		{
		case 'style1':
			$style = 'style1';
			break;            
		case 'style2':
			$style = 'style2';
			break;            
		case 'style3':
			$style = 'style3';
			break;    
		}
	}
print ""; 
?>


if(!empty( $_GET['theme'] ))
    {

This is saying if the "theme" variable is not empty than continue with the script. It reads the "theme" variable and stores the value.



$style = $_GET['theme'];

Set the value of "theme" into the "$style" variable. This can be left as a get variable, but it is easier to use a smaller, more clear variable.



switch( $style )
    {
    case 'style1':
        $style = 'style1';
        break;            
    case 'style2':
        $style = 'style2';
        break;            
    case 'style3':
        $style = 'style3';
        break;    
    }

This next piece of code is a "switch" statement. The "case" part is the string it looks for. For instance if $style had a value of "style3" than the script would load the 3rd stylesheet. The "break" says if the value of "$style" is found then jump out of the switch.



    }
print "<link rel=\"stylesheet\" type=\"text/css\" href=\"

This is a PHP "print" or "echo". It is outputting the first part of the code to load a stylesheet into a site.



    }
" . ( ( !$style ) ? 'main' : $style ) . ".css\" />"; 

This is a tertiary operator: 1?2:3, which means "if 1 is true, then 2, else 3". This means if the "$style" variable is not filled then load "main.css". Or if it is filled, load the selected style.



That is all there is to writing a style switcher in PHP

Follow Scriptplayground on Twitter (@scriptplay)

Comments: Style Switcher

 Ali  Thu Sep 1, 2005 12:26 am  
W00t, this is a pretty useful script and I think its just what I've been looking for.
 mkeefe  Sun Sep 4, 2005 9:39 pm  
Glad to hear that you liked and found use for this article. It is always nice to hear from people that appreciate your work. Keep in mind if you have an article idea you can simply request it. :)
 Haol  Thu Oct 27, 2005 12:55 am  
oooo, this is kl, very helpful i have laods of ideas that i can use this code for.
 Jhecht  Fri Jul 13, 2007 9:49 pm  
I used to use a javascript method to this... i believe all i did was find a link tag, then i'd check to see if the relationship attribute was "stylesheet", if it was, i'd update it with the separate stylesheet being clicked on. It used to work, not sure about it now.
 anonymous  Thu Feb 28, 2008 6:39 pm  
coded a full fledged theme engine in php.
http://tinyurl.com/yrb8yn (tinyurl link, the link is a bit long)
bye...
P.S. the demo may not work sometimes
 gaurav_m  Wed May 14, 2008 8:12 pm  
Nice !!!

like it
 Busby SEO Challenge  Sat Aug 2, 2008 8:33 pm  
Wow.. This is great! You can use this for creating different styles in WordPress.
 sms addict  Fri Jan 8, 2010 4:52 pm  
Great!
Add a comment
The ability to add new comments has been disabled in this archived version.

Loading
Main | Tutorials | Articles