Thursday, January 22, 2009

Php - how to switch css style sheet of a web page

Php - how to switch css style sheet of a web page

<?php
session_start();
$all_css = array();

$all_css['yellow']['file'] = "home_geel.css";
$all_css['blue']['file'] = "home_blauw.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_geel.css";
    break;
    case "blue":
    $css_file = "home_blauw.css";
    break;
    default:
    $css_file = "home_modern.css";
}
function style_switcher() {
    global $all_css;
    $style_links = "Style switch:&nbsp;\n";
    foreach ($all_css as $key => $val) {
        if ($_SESSION['css'] != $key) {
            $style_links .= "<a href=\"".$_SERVER['PHP_SELF']."?change_css=".$key."\">";
            $style_links .= "<b>".$val['label']."</b></a>&nbsp;&nbsp;\n";
        } else {
            $style_links .= "<b>".$val['label']."</b>&nbsp;&nbsp;\n";
        }
    }
    return $style_links;
}
?>
<!-- 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(); ?>

No comments: