Log in

View Full Version : Changing Theme



vividona
01-24-2009, 07:41 AM
I have many Themes in my Themes folder. What is the idea of changing theme through database. I mean is it possible to pickup one of theme from database?.

Thank you in advance

Schmoopy
01-24-2009, 08:31 PM
Well, the following code isn't exactly for a database but then I don't know why you'd want a database if you're going to be using stylesheets, why not just have them in a folder called "Themes" and then allow the user to select from one of them. Please try and explain how the database will be helpful in this situation.

Anyway, here's a simple style changer:


<?php
if (isset($_GET['theme']))
{
$theme = $_GET['theme'];
switch ($theme)
{
case 1:
$theme = 1;
break;
case 2:
$theme = 2;
break;
case 3:
$theme = 3;
break;
case 4:
$theme = 4;
break;
default:
$theme = 1;
}

}
?>
<html>
<head>

<link rel="stylesheet" type="text/css" href="theme<?php if(isset($theme)) { echo "$theme";} ?>.css"/>
</head>
<body>

<form action="themes.php" method="get">
<select name="theme">
<option value="1">Theme 1</option>
<option value="2">Theme 2</option>
<option value="3">Theme 3</option>
<option value="4">Theme 4</option>
</select><p></p>
<input type="submit" value="Select"/>
</form>


</body>
</html>

Twey
01-24-2009, 10:01 PM
Better:
<?php
session_start();

$themes = array(
'red' => 'Red',
'greenish' => 'Green/Blue',
'transparent' => 'Glass',
'fish' => 'Sea Life',
'grass' => 'Natural'
);

$defaultTheme = @$_POST['theme'];
if (!isset($themes[$defaultTheme]))
$defaultTheme = key($themes);

$_SESSION['preferred_theme'] = $defaultTheme;
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<link rel="stylesheet"
type="text/css"
href="<?php echo $defaultTheme; ?>">
<?php foreach ($themes as $file => $title): ?>
<link rel="alternate stylesheet"
type="text/css"
href="<?php echo $file; ?>.css"
title="<?php echo $title; ?>">
<?php endforeach; ?>
</head>
<body>
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<div>
<select name="theme">
<?php foreach ($themes as $file => $title): ?>
<option value="<?php echo $file; ?>">
<?php echo $title; ?>
</option>
<?php endforeach; ?>
</select>

<input type="submit" value="Select">
</div>
</form>
</body>
</html>Schmoopy, watch that pseudo-XHTML.

Schmoopy
01-24-2009, 10:57 PM
You mean the /> at the end of tags? :p

Twey
01-25-2009, 12:25 AM
Yes. In HTML, that technically ought to leave > characters scattered all over your page.

vividona
02-09-2009, 07:45 AM
Thank you so much friends for your help
you are No. 1