Results 1 to 4 of 4

Thread: How to set, define and change a sites stylesheet via PHP and use the defined value?

  1. #1
    Join Date
    Mar 2005
    Location
    Western Australia
    Posts
    148
    Thanks
    24
    Thanked 4 Times in 4 Posts

    Default How to set, define and change a sites stylesheet via PHP and use the defined value?

    Hi all

    I have been scouring the net and testing various PHP only methods to enable site visitors to set their preferred style (for example: https://github.com/justnorris/CSS-Style-Switcher, http://stackoverflow.com/questions/1...ie-and-echo-it etc), in this case i want to have a selection of either dark or light of which there is a dark.css and a light.css file in addition to a layout.css style sheet, the default will be the light one. Setting it up using that script is easy enough, however ...

    I want to be able to retrieve a value that defines what style sheet is being use, that could be the name or just a 0 or 1 value, so for example if the default or light is used the value is set to be 0, if the dark style is used the value is changed to 1 (or we can just use light and dark, really makes no difference.

    With that value defined I want to use it elsewhere on the page (site) so it loads content specific to the style chosen, but I cannot work out how to get that value and use it, therefore my request for help

    Thanks
    Last edited by gwmbox; 12-20-2014 at 12:52 AM. Reason: Resolved :)
    1st rule of web development - use Firefox and Firebug
    2nd rule - see the first rule
    --
    I like Smilies

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Something like this should work.

    Put this in the <head> section of your web page - the last line echos out a stylesheet so put it where you'd want that to appear;
    PHP Code:
    <?php 

    $default 
    'styles.css'// define stylesheets
    $darkcss 'dark.css';
    $lightcss 'light.css';

    $expire time()+60*60*24*30// how long to remember css choice (60*60*24*30 = 30 days)

    if ( (isset($_GET['css'])) && ($_GET['css'] == $lightcss) ) { // set cookie for light css
        
    $_SESSION['css'] = $_GET['css']; 
        
    setcookie('css'$_GET['css'], $expire);
        }

    if ( (isset(
    $_GET['css'])) && ($_GET['css'] == $darkcss) ) { // set cookie for dark css
        
    $_SESSION['css'] = $_GET['css']; 
        
    setcookie('css'$_GET['css'], $expire); 
        }

    if ( (isset(
    $_GET['css'])) && ($_GET['css'] == $default) ) { // set cookie for default css
        
    $_SESSION['css'] = $_GET['css']; 
        
    setcookie('css'$_GET['css'], $expire); 
        } 

    if (isset(
    $_COOKIE['css'])) { // check for css stored in cookie
        
    $savedcss $_COOKIE['css']; 
        } else {
        
    $savedcss $default;
        }

    if (
    $_SESSION['css']) { // use session css else use cookie css
        
    $css $_SESSION['css'];
        } else {
        
    $css $savedcss;
        } 

    // the filename of the stylesheet is now stored in $css

    echo '<link rel="stylesheet" href="/path/to/styles/'.$css.'">';

    ?>
    The filename of the stylesheet is stored in the $css variable so you can use this to insert style-specific content;
    PHP Code:
    <?php if ($css == 'styles.css') { ?>
        <p>DEFAULT CONTENT</p>
    <?php ?>

    <?php if ($css == 'dark.css') { ?>
        <p>DARK CONTENT</p>
    <?php ?>

    <?php if ($css == 'light.css') { ?>
        <p>LIGHT CONTENT</p>
    <?php ?>
    And these would be the links to switch / change stylesheets;
    PHP Code:
    <a href="?css=<?php echo $lightcss;?>">Light</a> 
    <a href="?css=<?php echo $darkcss;?>">Dark</a> 
    <a href="?css=<?php echo $default;?>">Default</a>
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. The Following User Says Thank You to Beverleyh For This Useful Post:

    gwmbox (12-20-2014)

  4. #3
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    I decided to document this in a blog post: http://www.dynamicdrive.com/forums/e...th-Save-Cookie

    More info and alternative uses there.
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  5. The Following User Says Thank You to Beverleyh For This Useful Post:

    gwmbox (12-20-2014)

  6. #4
    Join Date
    Mar 2005
    Location
    Western Australia
    Posts
    148
    Thanks
    24
    Thanked 4 Times in 4 Posts

    Default

    Wonderful and it works fantastic, many many thanks for your help
    1st rule of web development - use Firefox and Firebug
    2nd rule - see the first rule
    --
    I like Smilies

Similar Threads

  1. Replies: 3
    Last Post: 05-08-2012, 08:20 PM
  2. Define variables from PHP to JS
    By cascadeorca in forum JavaScript
    Replies: 0
    Last Post: 01-31-2011, 02:26 PM
  3. Replies: 1
    Last Post: 04-17-2007, 05:37 PM
  4. Define <h1> tag
    By geoffb in forum CSS
    Replies: 5
    Last Post: 12-30-2006, 02:51 PM
  5. define
    By merlyn in forum JavaScript
    Replies: 2
    Last Post: 01-10-2005, 06:37 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •