...

View Full Version : StyleSheet Selector



JamesI
04-25-2004, 01:19 PM
Hi, I was wondering if anyone knew of a script that would go in a dropdown list and choose a stylesheet. Basically, I want to give members the option to choose what stylesheet they use to browse the site, in a dropdown list, that will then stay that way until they change it or close the browser.

Does anyone know of one/can make one?

Thanks :)

raf
04-25-2004, 02:00 PM
Welcome here !

Very easy. You create a dropdown (lets call it stylesheetdropdown) with the stylesheetname as vaues. You post the selected value from the dropdown to a PHP page, where you then store it inside a sessionvariable. Like


session_start();
$_SESSION['stylesheet'] = $_POST['stylesheetdropdown'];

then on top of each page, you build your html header (preferably using an include) like this (inside the header tags:
<style type="text/css" media="all">@import "<?php echo $_SESSION['stylesheet']; ?>";</style>

JamesI
04-25-2004, 02:06 PM
I'm like, a PHP newbie, so please bear with me. :)

What else do I need to add to the code? Can you give me an example?

As it stands, I get this:

Notice: Undefined index: stylesheetdropdown in c:\program files\easyphp1-7\www\test.php on line 9


$_SESSION['stylesheet'] = $_POST['stylesheetdropdown'];

Thanks for your help :)

Nightfire
04-25-2004, 02:18 PM
<?php
session_start();
if(isset($_POST['submit'])){
$_SESSION['cssselector'] = $_POST['cssselector'];

switch($_SESSION['cssselector']){
case 'blue':
$css = "blue.css";
break;
case 'red':
$css = "red.css";
break;
}
}else{
$css = "default.css";
}
?>
<html>
<head>
<title>Style switcher</title>
<link rel="stylesheet" href="styles/<?php echo $css; ?>">
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
<select name="cssselector">
<option value="blue">Blue</option>
<option value="red">Red</option>
etc
</select>
<input type="submit" name="submit" value="Change style">
</form>
</body>
</html>

raf
04-25-2004, 02:20 PM
You don't need to add any PHP code. Just these two lines.

Is your dropdown with the stylesheet-options in called 'stylesheetdropdown' ? Did you post the form to this page ? Maybe post the relevant piece of the form here (the form-tag and the dropdowncode.

JamesI
04-25-2004, 02:58 PM
NightFire, great code!

I'm confused now, because I have it working exactly like that, with a blue.css and a red.css in the folder.

I then put the code into the document I want it in, and there's a style.css (what I changed default.css to), red.css, and blue.css. And all the styling from style.css disappears, and the selector doesn't work.

So I now have:
index.php

<?
session_start();
if(isset($_POST['submit'])){
$_SESSION['cssselector'] = $_POST['cssselector'];

switch($_SESSION['cssselector']){
case 'blue':
$css = "blue.css";
break;
case 'red':
$css = "red.css";
break;
}
}else{
$css = "style.css";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3 Transitional//EN">
<html>
<head>
<!-- TemplateInfo codeOutsideHTMLIsLocked="true" -->
<!-- TemplateBeginEditable name="doctitle" -->
<title>Alex-Simpson.com :: </title>
<!-- TemplateEndEditable -->
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!-- TemplateBeginEditable name="head" --><!-- TemplateEndEditable -->
<script language="JavaScript" type="text/JavaScript">
<!--


function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
//-->
</script>
</head>

<body>
<!-- Loads of body content -->
<td width="344">
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post" >
<select name="cssselector">
<option value="blue">Blue</option>
<option value="red">Red</option>
etc
</select>
<input type="submit" name="submit" value="Change style">
</form>
</td>
</tr>
<tr>
<td height="5"></td>
<td></td>
</tr>
</table>

</td>
</tr>
</table>
</body>
</html>

A blue.css, red.css, and style.css in the folder. And it's just not working - see here (http://81.131.136.224/Alex/index.php).

Thanks a lot for your help. :)

Nightfire
04-25-2004, 04:24 PM
You've not added the link to the css
<link rel="stylesheet" type="text/css" href="stylesfolder/<?php echo $css;?>">

JamesI
04-25-2004, 04:53 PM
Working like a dream. :)

Thank you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum