Group: *Premier*
Posts: 85
Joined: 28-November 04
From: London
Member No.: 1,218
Wouldn't it be cool if you could produce a random page design for your visitors each time they refreshed the page? Of course!
This is a very simple tutorial teaching you how to generate random page designs using the power of PHP.
Skill needed: Beginner - Intermediate
=============================================
Let's Begin
First of all, create a new PHP page in your text editor. Begin by copying pasting the following:
CODE
<?php
$styleno = 1;
$stylerand = rand (0, $styleno);
?>
In English, it is basically:
$styleno = 1; The number of stylesheets you have made. Remember, due to the way languages like PHP count, your numbers will start with zero. So add up the number of stylesheets you have and subtract one. Complicated? Not really when you think about the excellent results.
$stylerand = rand (0, $styleno); This creates a variable which has a random number as a value. If you haven't used it before, the 'rand' function takes a starting number (0 in this case) and generates a random number using digits up until the specified end (a variable in this case). The $styleno at the end inserts the number of stylesheets you specified earlier.
We'll get onto naming the stylesheets later.
Next we'll need to start the html part of the page.
Of course if you want you can add all of your meta tags and doctype declarations but i've kept it simple for brevity.
The bit you need to focus on is the ''
What that is basically saying is 'include a stylesheet with a randomly numbered name', it's easier to give an example than to explain; Before we generated a random number, this number will be used in your stylesheets. For instance, if you generate the number 3, this script will include stylesheet 3 or style3.css.
Which brings me on to stylesheet numbering. Basically, you have to name you stylesheets 'style' then a number '.css'. E.g style1.css, style2.css. But, as I mentioned before, the numbering starts with zero so you would begin style0.css, style1.css and so on.
One last step. After the head insert your tags and all your beautiful CSS powered content.
==========================================
Here's the script as a whole if you want to just copy and paste:
Group: Member
Posts: 13
Joined: 26-February 07
Member No.: 15,155
Cool tutorial. I agree that while each page being random would be fun, you should have a way to lock the style on one you want, maybe setup a session variable such as:
CODE
if ($_REQUEST['style'] != "") //add some validation so people couldn't break the page. $_SESSION['style'] == $_REQUEST['style'];
someone could click "Save this style" and it would load the page with page.php?style=2