[Tutorial] Random CSS with PHP

Example of PHP code.

Moderator: Moderators

[Tutorial] Random CSS with PHP

Postby Greg on Sun Feb 27, 2005 7:49 pm

Hey.

Ever wanted a random CSS? If so read on.

Using PHP it is very easy to have a random style sheet. Use the following code

Code: Select all
<link rel="stylesheet" type="text/css" href="<?php
$stylesarray = array("red","blue","black");
echo $stylesarray[rand(1,3)];
?>.css">


Where it says red, blue and black, change it to your style sheets. If you have more than 3 eg 5 change [rand(1,3)]; to [rand(1,5)];

So when a user loads up a page, it could load black.css or red.css etc.

Thanks,
Greg
User avatar
Greg
Registered User
Registered User
 
Posts: 651
Joined: Wed Jul 07, 2004 3:20 pm
Location: Yorkshire, United Kingdom

Postby UseLess on Mon Feb 28, 2005 5:05 am

Greetings,

There is a problem with the code above, array indexes start at 0 and not 1 and the count of items in the array is 3, but the 3rd item has an index of 2 so when 3 is the random number selected nothing is output. And the item at index 0 would never be shown...

Therefore....

Code: Select all
<link rel="stylesheet" type="text/css" href="<?php 
$stylesarray = array("red","blue","black"); 
echo $stylesarray[rand(0,count($stylesarray)-1)]; 
?>.css">


Solves it...
Movie Quote:
It's not the years honey, it's the mileage...

I do not provide any install services for phpBB, Mods or Styles.
Please do not pm me for support/scripting help - you won't get any reply. If you have a question then make a post in the appropriate forum.
User avatar
UseLess
Registered User
Registered User
 
Posts: 6220
Joined: Mon Sep 27, 2004 2:14 am
Location: North East, UK

Postby robbie on Mon Mar 28, 2005 12:51 am

array functions have an inbuilt randomizing function.

Code: Select all
<link rel="stylesheet" type="text/css" href="<?php stylesarray = array("red","blue","black"); echo $stylesarray [ array_rand($stylesarray) ]; ?>.css">


or use the better random number generator: mt_rand()

Code: Select all
<link rel="stylesheet" type="text/css" href="<?php 
$stylesarray = array("red","blue","black"); 
echo $stylesarray[mt_rand(0,count($stylesarray)-1)]; 
?>.css">
User avatar
robbie
Coder
Coder
 
Posts: 67
Joined: Fri Mar 12, 2004 4:40 am
Location: New Zealand

Postby Basilakis on Sun Apr 03, 2005 5:04 pm

Does anyone have any idea whow can we do that in phpbb... A drop down menu? And if user is loged to store the template he choose!
Basilakis
Registered User
Registered User
 
Posts: 113
Joined: Sat Oct 09, 2004 4:40 pm

Postby DoubleJ on Sun Apr 03, 2005 7:17 pm

What you should do if you want to randomize this in you phpBB
Put the randomize code in your includes/page_header.php
And make sure that the
Code: Select all
'T_HEAD_STYLESHEET' => $theme['head_stylesheet'],

$theme['head_stylesheet'] is replaced with the variable you generated out of this script.
It never hurts to help.....
[ My phpBB Mods and Styles ]
My Mod: [ Simple Shoutbox Mod ]
[ DoubleJWeb ] [ Get Opera ]
User avatar
DoubleJ
Moderator
Moderator
 
Posts: 716
Joined: Sat Jan 22, 2005 9:58 pm
Location: DoubleJWeb.Net

Postby Basilakis on Mon Apr 04, 2005 5:16 pm

ANd how can i make links so users can choose what style they want to use? ANd store this at the cookies?
Basilakis
Registered User
Registered User
 
Posts: 113
Joined: Sat Oct 09, 2004 4:40 pm

Postby DoubleJ on Mon Apr 04, 2005 5:29 pm

[quote user="Basilakis" post="27000"]ANd how can i make links so users can choose what style they want to use? ANd store this at the cookies?[/quote]

Well I don't know about the cookies.
But the change style mod from ktauber is a good one.
Although you have to make your own script if you dont want to insert all styles manually into the header(for the choosebox)
you can get it from:
www.ktauber.com
It never hurts to help.....
[ My phpBB Mods and Styles ]
My Mod: [ Simple Shoutbox Mod ]
[ DoubleJWeb ] [ Get Opera ]
User avatar
DoubleJ
Moderator
Moderator
 
Posts: 716
Joined: Sat Jan 22, 2005 9:58 pm
Location: DoubleJWeb.Net


Return to PHP Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest