Hi
After doing various tutorials I came up with a stylesheet switcher with php. It is similar to the one on alistapart.com. only is uses an array for storing the stylesheets.
<?php
header("Cache-Control: no-cache, must-revalidate");
if ($_GET["action"] == changestyle) {
$style = $_GET["linkstyle"];
setcookie("pagestyle", "$style", time()+40000000);
$return = $_GET["return"];
header("Location: $return");
exit;
}
if ($_GET["action"] == deletecookie) {
setcookie ("pagestyle", "", time() - 3600);
$return = $_GET["return"];
header("Location: $return");
exit;
}
$styles=array(
"normal" => "normal.css",
"horizontal" => "horizontal.css",
"retro" =>"retro.css"
);
$pagestyledefault = "normal.css";
/* Would like to have code in the $styles array whereby you can set the default as => default, but I don't want to have to have the => for the others */
$pagestyle = $_COOKIE['pagestyle'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="description" content="Page description" />
<?php
if($_COOKIE["pagestyle"] == "") {
echo "<link rel=\"stylesheet\" href=\"$pagestyledefault\" media=\"screen\" />";
} else {
foreach($styles as $title => $file){ echo "<link rel=\"stylesheet\" href=\"$pagestyle\" media=\"screen\" />";
}
}
?>
</head>
<body>
<div id="outline">
<div id="header">
<h1>Header</h1>
</div>
<div id="navigation">
<ul>
<li>Nav1</li>
<li>Nav2</li>
</ul>
</div>
<div id="subtitle">
<h2>Sub title</h2>
</div>
<div id="pagecontent">Page content</div>
<div id="footer">
<fieldset>
<legend>page style</legend>
<ul>
<?php foreach($styles as $title => $file){
$return = basename($_SERVER['REQUEST_URI']);
echo "<li><a href=\"$return?action=changestyle&linkstyle=$file&return=$return\" onclick=\"this.innerHTML = 'please wait...'\">$title</a></li> "; } ?>
</ul>
<br />
<?php if($_COOKIE["pagestyle"] == "") { echo "Hi. You must have cookies enabled to use this"; } else {
echo "Cookie information: $_COOKIE[pagestyle] | <a href=\"$return?action=deletecookie&return=$return\" onclick=\"this.innerHTML = 'Deleting...'\">Delete</a>"; } ?>
</fieldset>
</div>
</div>
</body>
</html>
I think the $return variable could be placed above the header location script like the alistapart one does.