Previous Message
Next Message

Re: [css-d] Backward-compliant style switcher (in PHP)

Sent by Geoff Sheridan on 10 July 2002 16:04


If anyone wants to do it in PHP rather than ASP or Javascript I wrote 
this. It seems (to me) much simpler than the javascript switcher and 
is browser independent.
See it in use here: http://www.nvsc.org.uk (I know, I know - tables - naughty)

The same code goes on every page. Page is reloaded when style link is 
clicked. Choice is stored in a cookie, so is retained throughout the 
site. Toggles between two styles, 'large' and 'default', but might be 
easily customised for more choices.

You need a PHP enabled server, and the privileges to run php scripts. 
For the choice to be remembered from page to page, user must have 
cookies enabled.

I will be writing a tutorial on it soon but for now the code is:

*before the html tag (Must be before any html because it can only 
then set a cookie):
	<?php
	include("styleswitch.php");
	?>

*in document head, this prints the name of the chosen stylesheet into the html:

	@import "<? print $csstyle?>.css";

...or use <link> for NN4 compatibility.

*Somewhere in the document, the links which switch the style:
	<span class="large">[<a title="Visually impaired people may 
view this page with BIGGER text" href="<? print 
$PHP_SELF."?newstyle=large"?>">larger text</a>]</span>
	<span class="default">[<a title="Back to normal font size" 
href="<? print $PHP_SELF."?newstyle=default"?>">normal 
text</a>]</span>

*In file styleswitch.php:
	<?php
	//process any new request
	if ($newstyle){
	$csstyle=$newstyle;
	setcookie("csstyle", $csstyle, time()+360000, "/", "nvsc.org.uk", 0);
	}

	//set to default if not specified
	if ( !isset ($csstyle)){
	$csstyle="default";
	}
	?>

In order that the link for the stylesheet your user is already using 
does not show, in file large.css:
.large{display:none;}
& in default.css:
.default{display:none;}

That's all folks. Hope that wasn't too off topic.

Geoff



>  > From: [EMAIL-REMOVED]
>>
>>  >Why not use PHP?  Any CSS-compliant browser will see the correct
>>  >results, and it'll actually work in every browser (you just won't see
>>  > anything in non-CSS ones :-)).
>>
>>  If only I knew PHP, and had any idea at all how to write, set up, or
>>  use it :)
>>
>>  Another day. It's on my agenda. Until then, I'm working with JS, which
>>  I don't know either, but which I can at least use.
>
>without seeing the start of this thread, i don't know if the PHP
>suggestion was based on your server environment, or just a
>language suggestion, but there is a handy tutorial on how to do it in
>ASP over at evolt.org:
>
>Style Switcher in ASP
>http://evolt.org/article/css-d/17/28241/
>
>>  But this must have been a REALLY boring question or something, because
>>  everybody seems to be ignoring me... :)
>
>nah, i just didn't see it...
Previous Message
Next Message

Message thread: