Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Randomly Generated Page, PHP - Beginner - Intermediate
Adam
post Mar 30 2005, 02:17 PM
Post #1


Regular Dot
Group Icon
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.

Underneath the PHP you just inserted, add

CODE
<html>
<head>
<link rel="stylesheet" type="text/css" href="style<?=$stylerand ?>.css" />
<title>Random Style</title>
</head>


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:

CODE
<?php

$styleno = 1;

$stylerand = rand (0, $styleno);

?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="style<?=$stylerand ?>.css" />
<title>Random Style</title>
</head>

<body>

<!-- Content goes here -->

</body>
</html>


==========================================

Well that's it! You got through it. Hopefully i've explained this well and it should be easy for you.
Any problems, just reply and I'll try to help.
 
+Quote Post  Go to the top of the page
*D*
post Mar 30 2005, 11:05 PM
Post #2


Merry KissMoose
Group Icon
Group: Main Team
Posts: 15,988
Joined: 18-May 04
From: North Pole
Member No.: 2





hiya.gif Hi Adam, didn't know you was a member here, thankyou for this tut will be helpful to some.
 
+Quote Post  Go to the top of the page
RedDragon
post Mar 31 2005, 02:39 AM
Post #3


insert coin
Group Icon
Group: Main Team
Posts: 5,344
Joined: 24-May 04
From: Maastricht
Member No.: 35





Thank you Adam!

Glad you agreed on my request thumbsup.gif


IPB
 
+Quote Post  Go to the top of the page
Eddie_K
post Mar 31 2005, 03:54 AM
Post #4


Hush now, I'm thinking...
Group Icon
Group: *Premier*
Posts: 3,977
Joined: 25-May 04
From: Heaven
Member No.: 47





Thanks for puttin this up for us Adam bigwink.gif

Great stuff!

Eddie


IPB
Take the time to relax. Enjoy a game of Fallen Sword, great multiplayer RPG!

 
+Quote Post  Go to the top of the page
Adam
post Mar 31 2005, 05:07 AM
Post #5


Regular Dot
Group Icon
Group: *Premier*
Posts: 85
Joined: 28-November 04
From: London
Member No.: 1,218





No probs, I'll post an addition when I have time showing you how to 'bookmark' your favourite styles using cookies.
 
+Quote Post  Go to the top of the page
Iceshifter
post Jun 14 2005, 10:05 AM
Post #6


New Member
Group Icon
Group: Member
Posts: 3
Joined: 14-June 05
Member No.: 4,705



cool tut.. thanks for making

This post has been edited by Iceshifter: Jun 14 2005, 10:07 AM
 
+Quote Post  Go to the top of the page
TheNTW
post Jan 8 2007, 07:15 AM
Post #7


New Member
Group Icon
Group: Member
Posts: 3
Joined: 7-January 07
Member No.: 14,436





Can you include how to add not randomly but so i could select what style i want to use?
 
+Quote Post  Go to the top of the page
mwookie
post Feb 26 2007, 12:06 PM
Post #8


New Member
Group Icon
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

then in the code it could be:

CODE
if (isset($_SESSION['style']))
{
  $stylerand = $_SESSION['style'];
}
else
{
   $styleno = 1;

   $stylerand = rand (0, $styleno);

}


Kind of sloppy but it could work.

____________________________
"You cannot escape the responsibility of tomorrow by evading it today." Abraham Lincoln
Company – Projects (New Mexico Real Estate ListingFamily Friendly Image Search)
 
+Quote Post  Go to the top of the page

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

RSS Lo-Fi Version Time is now: 21st November 2008 - 02:38 AM
Southport Hotels | Credit Cards | Credit Card Consolidation | Mortgages | Loan