Friday, October 5, 2012

Switch between multiple CSS styles sheets with PHP

10/05/2012

A lot of webmasters like this nice looking CSS websites with the possibility to switch between different CSS style sheets. This code snippet will help to create the switch. Just create you different style sheets and your universal coded html documents (these one have to work with all the style sheets) and at least use this code in all html documents (or with a PHP included file). Notice: the links at the bottom of this site; they are created with the same function. After a visitor changed the style, his choice is stored inside a session variable.
<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/


session_start();
$all_css = array();


$all_css['yellow']['file'] = "home_yellow.css";
$all_css['blue']['file'] = "home_blue.css";
$all_css['modern']['file'] = "home_modern.css"; // default

$all_css['yellow']['label'] = "Yellow!";
$all_css['blue']['label'] = "Deep blue";
$all_css['modern']['label'] = "Final..."; // default

$default_value = "modern"; // set the default value here

if (isset($_GET['change_css']) && $_GET['change_css'] != "") {
$_SESSION['css'] = $_GET['change_css'];
} else {
$_SESSION['css'] = (!isset($_SESSION['css'])) ? $default_value : $_SESSION['css'];
}
switch ($_SESSION['css']) {
case "yellow":
$css_file = "home_yellow.css";
break;
case "blue":
$css_file = "home_blue.css";
break;
default:
$css_file = "home_modern.css";
}
function style_switcher() {
global $all_css;
$style_links = "Style switch: \n";
foreach ($all_css as $key => $val) {
if ($_SESSION['css'] != $key) {
$style_links .= "";
$style_links .= "".$val['label']."  \n";
} else {
$style_links .= "".$val['label']."  \n";
}
}
return $style_links;
}




?>




Usage:


<!-- EXAMPLE: place this inside your html header -->
<link href="/includes/<?php echo $css_file; ?>" rel="stylesheet" type="text/css">
<!-- place this code inside the body where you want to show the links -->
<?php echo style_switcher(); ?>




helpful? Share this

The Editorial Team of 4everTutorials consists of a group of PHP Professionals.

0 comments:

 

© 2014 4everTutorials. All rights resevered.

Back To Top