Loading a random Cascading Styles Sheet

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

nimmØ2
Forum Newbie
Posts: 14
Joined: Thu Feb 08, 2007 3:55 am

Loading a random Cascading Styles Sheet

Post by nimmØ2 »

I'm not sure if I'm using the right terminology, nor whether I'm using it correctly or not.
I intend to create several color schemes. Each time a page loads, I want it to load one randomly.

Thanks for your time.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

make an array of CSS files (including paths) and use array_rand() to write one to your <link rel="stylesheet">
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Beware: Changing colors like that is something that we usually avoid in website design from page to page unless the user is given a choice. Just know that by doing what you're doing, try not to make the colors too different (or too bright... That'd just hurt the users eyes!)
nimmØ2
Forum Newbie
Posts: 14
Joined: Thu Feb 08, 2007 3:55 am

Post by nimmØ2 »

haha, thanks.
Mostly I'm just playing with things.
But I'll keep that in mind.

Could you possibly post snippets of code?
I'm very inexperienced, but don't want others writing all the code for me.
I want to learn.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

Code: Select all

//untested
$cssFiles = array('fileone.css','filetwo.css','filethree.css');
$randomCssFile = array_rand($cssFiles);
echo '<link href="' . $randomCssFile . '" rel="stylesheet" type="text/css" />';
nimmØ2
Forum Newbie
Posts: 14
Joined: Thu Feb 08, 2007 3:55 am

Post by nimmØ2 »

nickvd wrote:

Code: Select all

//untested
$cssFiles = array('fileone.css','filetwo.css','filethree.css');
$randomCssFile = array_rand($cssFiles);
echo '<link href="' . $randomCssFile . '" rel="stylesheet" type="text/css" />';
Ah, now could someone please explain how this line of code works?

Code: Select all

echo '<link href="' . $randomCssFile . '" rel="stylesheet" type="text/css" />';
Is that echo I see? I'm unsure of what is to be displayed.
I've got the link, the concatination, and the varialbe, I think the 'type="text/css" is html, but am not sure how it works.
I also have no clue where rel="stylesheet" comes into play.

Thanks for the response, nick.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Code: Select all

<link href="/some/stylesheet.css" rel="stylesheet" type="text/css" />
That's the html to include a stylesheet.

The echo and the variable are PHP.

Check out http://w3schools.com for an intro to html and/or css.

Also, your signature could be summed up as: "I ramble" ;-)
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

Kieran Huggins wrote: Also, your signature could be summed up as: "I ramble" ;-)
I like that.
nimmØ2
Forum Newbie
Posts: 14
Joined: Thu Feb 08, 2007 3:55 am

Post by nimmØ2 »

So an external style sheet is loaded into my document.

I think I made the mistake of learning the little html I know through Frontpage.
While Frontpage made mediocre websites suitable for Middle School Project or High School Assignments,
it's flexibility is obviously limited, and I think I missed out on getting a solid foundation on html.

I'm reading up on how to create and use Cascading Style Sheets now.
What software would you guys recommend for using them within a website?
Dreamweaver?
Or Notepad. I'm not sure if this is something to be hand-coded or not.
Thanks again.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Choose any editor with syntax highlighting - and you will be fine for the most part.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

When it comes to stylesheets, you could use Notepad and be as well off as anyone else. As long as you know the properties, and are aware of how easy it is to add an extra semicolon or bracket by accident, you'll be fine.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

There's a whole thread on php/html/css editors somewhere, I use EditPlus.

Work through the w3schools tutorials and try stuff - you'll pick up what you're missing.
nimmØ2
Forum Newbie
Posts: 14
Joined: Thu Feb 08, 2007 3:55 am

Post by nimmØ2 »

mm.
Wish me luck. :D
nimmØ2
Forum Newbie
Posts: 14
Joined: Thu Feb 08, 2007 3:55 am

Argh.

Post by nimmØ2 »

Didn't quite work. Likely to be a mistake on my part.

Here's my code.

index.php

Code: Select all

<html>
<header>
<title>Here comes the date again.</title>
<?php
include("header.php");
?>
</header>
<body>
<h1>Roar!</h1><br>
<h2>Roar!</h2><br>
<p>Roar!</p><br>
<l>Roar!</l><br>
<kindaskinnytext>kindaskinnytext</kindaskinnytext><br>
<skinnytext>skinnytext</skinnytext><br>
<reallyskinnytext>reallyskinnytext</reallyskinnytext>
</body>
</html>
header.php

Code: Select all

<?php
$cssFiles = 
//I'm not sure how to put quotation marks into a string, so I put the entire url instead of a relative one.
array('http://www.nimm02.com/workspace/css/black.css','http://www.nimm02.com/workspace/css/white.css','http://www.nimm02.com/workspace/css/grey.css');
$randomCssFile = array_rand($cssFiles);
echo '<link href="' . $randomCssFile . '" rel="stylesheet" type="text/css" />';
echo 'Hi!';
?>
black.css

Code: Select all

body {background-color: black}
kindaskinnytext {letter-spacing: -1px}
skinnytext {letter-spacing: -1.5px}
reallyskinnytext {letter-spacing: -2px}
h1 {color: gray}
h2 {color: darkgray}
p {color: White}
l {color: lightGray}
/*  Both white.css and grey.css are very similar with different color values. */

After a bit of troubleshooting I tried using this as my header.php:

Code: Select all

 <?php
echo 'Roar.';
?>
And found that it wasn't displaying even that.
I do know that my host supports php, as I've tested other things.
Help would be appreciated.
The output can be found at http://www.nimm02.com/workspace/css/

Thank you kindly.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The output right now is showing different results than what the above PHP should be showing.

But there are a few things to note: There's no <header>, <l>, <kindaskinnytext>, <skinnytext>, or <reallyskinnytext> tags in the standards.

Your code appears to be confused as to what it's using. Are you using HTML 4, or XHTML?
Post Reply