Random PHP Templates?

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

Post Reply
Fuente
Forum Newbie
Posts: 3
Joined: Fri Nov 14, 2003 9:39 am

Random PHP Templates?

Post by Fuente »

Hi all, :D

I've got two versions of one page - One with a black background/logo/graphics and one light grey background/logo/graphics, and I want make them load up randomly with either one each time someone visits.

However, the images on each page are tailored to that color (black logo for the black page, grey logo for the grey page), and a few of the images have javascript link code attached to them, so I can't just use a CSS theme to change the page.

So is there any way I can code a php page which holds both black and grey templates (perhaps just the code between the <body>...</body> tags) and include some PHP code that selects to load either section of the code randomly each time the page is loaded?

I would also like to be able to link to each page like this:
index.php?black - For the black page, and...
index.php?grey - For the grey page.

Would this be possible?

Cheers :D
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

very possible.

I suggest something like this: index.php?version=black

Code: Select all

&lt;?php

$valid_versions = array('black'=&gt;'black','grey'=&gt;'grey','gray'=&gt;'grey'); // allow for spelling variance a bit...

if(isset($_GET&#1111;'version']) &amp;&amp; isset($valid_versions&#1111;strtolower($_GET&#1111;'version'])]))
{
  $version = $valid_versions&#1111;strtolower($_GET&#1111;'version'])];
}
else
{
  $version = $valid_versions&#1111;array_rand($valid_versions)];
}

// now use $version to figure out which one to load
include('templates/mysite.' . $version . '.php');
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

and to add to feyds suggestion, I'd then use the $version to open all images and such by putting them in a new folder.

ie:
http://site.com/index.php
http://site.com/black/header.jpg
http://site.com/black/style.css
http://site.com/grey/header.jpg
http://site.com/grey/style.css

Code: Select all

$num = rand(0,1);
$version = ($num==0)?"grey":"black";
Post Reply