• Switch between multiple cascading style sheets (CSS) using PHP

    Please check the below code snippet that will help you to create switch between different css. First create different style sheets and your universal coded html documents (those 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). The links at the bottom of this site are created with the same function. After a visitor changed the style, his choice will be stored inside a session variable.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    
    <!--?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']) &#038;&#038; $_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="\&quot;&quot;.$_SERVER['PHP_SELF'].&quot;?change_css=&quot;.">";
                $style_links .= "<b>".$val['label']."</b></a>  \n";
            } else {
                $style_links .= "<b>".$val['label']."</b>  \n";
            }
        }
        return $style_links;
    }
    ?&gt;
    <!-- EXAMPLE: place this inside your html header -->
    			<link href="/includes/&lt;?php echo $css_file; ?&gt;" rel="stylesheet" type="text/css" />
    <!-- place this code inside the body where you want to show the links -->
    <!--?php echo style_switcher(); ?-->

Comments on this post

Leave a Reply

  • Security Code :


    six × = 54