tags:

views:

181

answers:

4

I would like to add a feature onto my site that would allow a user to choose between multiple styles. I am quite new to PHP but I'm guessing it would be able to be done. I've seen this feature on other sites. How would I go about doing this or could somebody refer me to a tutorial or guide on how this can be done? Thank you.

+5  A: 

To start you off, you could simply make the stylesheet link(s) dynamic:

<link rel="stylesheet" href="<?=$style?>" type="text/css"/>

And provide links that change them:

<a href="/page.php?theme=racecar">Racecar</a>

On the server, assign $style according to the query string, it would also be a good idea to default to something in case the user decides to modify the URL:

<?php
    $stylesArr = array('racecar', 'magenta', 'cartman');
    if(isset($_GET['theme']) && in_array($_GET['theme'], $stylesArr)) {
        $style = $_GET['theme'] . '.css';
        setcookie("theme", $style, time()+(3600*24*30));//expires in one month
    } else {
        if(isset($_COOKIE['theme']) && in_array($_COOKIE['theme'], $stylesArr)) {
            $style = $_COOKIE['theme'] . '.css';
        } else {
            $style = 'default.css';
        }
    }
?>
<link rel="stylesheet" href="<?=$style?>" type="text/css"/>

You can propogate the user's style via the query string, cookies or sessions.

karim79
If the user goes to another page on the site, won't this change the style?
Nate Shoffner
@Nate Shoffner - I've edited to write and read from a cookie. It's not tested, but I think it should work.
karim79
Nate Shoffner
@Nate Shoffner - isset() was missing the closing bracket. I've fixed it.
karim79
I've been trying to tinker with it, but I can't get the actual styles to work. I've made the necessary changes to the names to match the pre-existing style sheets and they are in the correct directory but no styles are being applied.
Nate Shoffner
@Nate Shoffner - make sure your paths are all okay, echo out the $style at the different stages of execution to try to debug it. If I can get the time I'll try to test it myself.
karim79
Nate: Check the resulting HTML; is the link to the style being changed?
Noon Silk
Okay, I tried what you said and it worked when I changed it too:<link rel="stylesheet" href="<?php echo $style?>" type="text/css"/> So it does display the correct style, but will this save it so that the next time the user visits the page it will display the last style sheet?
Nate Shoffner
@Nate Shoffner - That means you don't have short tags enabled, guess I shouldn't have assumed. You can test that by restarting the browser and opening the page again to see if the same style is being applied.
karim79
Just tried it and when I open the page, it displays the default theme.
Nate Shoffner
Any idea as to why it's not loading the correct style sheet after I restart the browser?
Nate Shoffner
@Nate Shoffner - try it now, I'm pretty sure I've fixed the problem
karim79
Hmmmm, for some reason, still not working. The cookie is there, I've checked. It contains the correct path to the stylesheet, but it's almost like it's not reading it?
Nate Shoffner
A: 

You can store information about style in cookies, or in database if users are regitered. And then depending on value add required css file on page with php.

x2
How would I store them in a cookie?
Nate Shoffner
A: 

Depending on how your pages are rendered it might be best to store the theme in a cookie once a theme has been selected otherwise you need to append ?theme=racecar to every link on your page.

if(isset($_GET['theme']))
{
    setcookie('theme', $_GET['theme']);
    $style = $_GET['theme'];
}
else if(isset($_COOKIE['theme']))
{
    $style = $_COOKIE['theme'];
}
else
{
    $style = 'default';
}

<link href="/styles/<?php echo $style; ?>.css" rel="stylesheet">
rojoca
?theme="><script>alert('I win?');</script>
Daniel
See @karim79's edited answer for safer version
rojoca
A: 

I've also seen it done with javascript.

Same basic idea as a few other answers: you write the chosen style to a cookie, and then read that when the page loads to determine which stylesheet to load.

With javascript, you have the added benefit of being able to switch styles without reloading the page, and if you set up your css nicely, you can switch something like the body's id or class and have a new style without having to download a new stylesheet, so the style-switch happens almost instantly.

Very sweet effect, good luck with the implementation.

nilamo