Jump to content

sessions and styles


Marty Walker
 Share

Recommended Posts

Hi,

I've been reading up a bit on PHP style swtichers (more for the purposes of getting sign-off on a certain look, colour schemes etc) but I can't seem to get any of them working the way I'd like with PW. Does anyone have and pointers for rolling one with PW sessions or cookies?

Regards

Marty

Link to comment
Share on other sites

Best cross-browser way I've found is a bit of jQuery and cookies.

If you set your base stylesheet up, then all your colour variations as separate stylesheets, but tie them to a unique body id (so if you have an orange scheme, set all your styles in your orange stylesheet to be prefixed with #orange for example) then you just need to include ALL the various stylesheets in your <head> area and use some jQuery to change the body ID on a button click.

Something like (untested):

$('#orangebutton.click(function () {
$('body').attr('id', 'orange');
});

Then your orange styles would kick in automatically. You'd also need some cookie code too in your JS to save that, but I'm typing quickly and don't have that code to hand - Google jQuery and cookies, but I think the code I used was just vanilla Javascript functions.

EDIT: Forgot to mention that when they move to any other page from here, you could create a session variable based on that cookie too if you like - this way with Javascript means the page doesn't have to reload though. I've done both on one site once before, which could be a bit overkill.

Link to comment
Share on other sites

I wasn't sure what first what you meant by switcher, but now I think I get it. Assume I understand correctly, you need something to target the switch like a GET variable, i.e. domain.com/?theme=pretty

Then before you send any output (like before your <doctype>) you'd check for that GET variable and stuff it in the session.

if($input->get->theme) {
 // set the theme
 $session->theme = $sanitizer->pageName($input->get->theme); 
}

Now that value in $session->theme will be retained regardless of what page they click on, or at least until they set another.

When you get into the <head> part of your document where you are going to be outputting the stylesheet links, you would just check the value of $session->theme:

if($session->theme == 'grunge') {
 $css = 'ugly.css'; 
} else if($session->theme == 'pretty') {
 $css = 'baby-unicorn.css'; 
} else {
 $css = 'default.css'; 
}

echo "<link rel='stylesheet' type='text/css' href='{$config->urls->templates}styles/$css' />";
  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...