decadence
09-14-2004, 08:38 AM
Currently, I am developing a CSS stylesheet set, which I wish to incorporate in a manner which will load a randomly selected (from the set) layout upon each new visit to the page. However, I am totally stymied. Can anyone offer me some suggestions on this, or point me to some online tutorials?
Thanks all,
-deca
Ed Hall
09-14-2004, 10:56 AM
http://www.alistapart.com has a good tutorial on how to have alternative stylesheets... you can also try google and search for random stylesheet onload.. that's what I did and found some great scripts that work.. only problem is that it needs to be done with PHP or CGI(sigh)
decadence
09-14-2004, 01:09 PM
Oh ok, well I use php as well, so that helps a ton. Thank you :grin2:
Koobi
09-14-2004, 01:22 PM
Assuming you have two stylesheets, you can do this:
<?php
switch(date('U') % 2)
{
case 1:
$style = 'randomOne.css';
break;
default:
$style = 'randomTwo.css';
}
?>
.
.
.
.
<style type="text/css" media="screen">@import "<?=$style?>";</style>
.
.
.
.
You can change the value within the braces next to "switch" according to the number of style sheets you have.
The percent sign (%) means the Modulus. So, 3 % 2 = 1 since the remainder of 3 divided by 2 is one.
date('U') is the nuber of seconds elapsed since the Unix epoch so that number is continuously increasing.
Let me know if anything doesn't make sense :)
:edit:
I forgot to mention that case 1: tells PHP that if date('U') % 2 is equal to one (which is the value next to the word case), then equate $style to randomOne.css and if date('U') % 2 does NOT equal one, then by ddefault, assign randomTwo.css to $style.
NOTE: You do not need a break; after a case if that case is the last.
Assuming you had 5 stylesheets for example, your code would look something like this:
<?php
switch(date('U') % 6)
{
case 1:
$style = 'randomOne.css';
break;
case 2:
$style = 'randomTwo.css';
break;
case 3:
$style = 'randomThree.css';
break;
case 4:
$style = 'randomFour.css';
break;
default:
$style = 'randomFive.css';
}
?>
.
.
.
.
<style type="text/css" media="screen">@import "<?=$style?>";</style>
.
.
.
.
:EDIT:
And another thing, try and use switch/case more than if conditions because switches are slightly faster. heh heh that's a lot of editing for one post :D
Arch Stanton
09-15-2004, 08:40 AM
I would use a small style sheet that has the randomization, and another style sheet for the things that stay the same. Or use an internal style (using the style tag) to do the randomization (this is all assuming your using php). Doing this will make sure the majority of your style sheet can be cached by the browser and reused on every page.