22/10/03

Comments 5

php snippet

I’ve found the PHP stylesheet switcher from ALA immensely useful, as it also opens up an easy way to provide different content, depending on the style chosen.

For example, to make sure that the navigation is displayed ‘flat’ in the diet version of hicksdesign (by removing the javascript), I’ve used the following php snippet:


<?php if ($sitestyle=="plain") echo "<script type=\"text/javascript\" src=\"/js/global_plain.js\"></script>"; else echo "<script type=\"text/javascript\" src=\"/js/global.js\"></script>";
?>

If ‘plain’ has been chosen by the user, it inserts the link to an external javascript file containing no menu scripts. If the user has chosen the default, or not chosen at all, it inserts the link to the version that has. (This won’t work in Mozilla if the user changes style using the browser menu). Using the css property display:none will just hide it from view, but this means you can decide what content goes into the HTML before it actually gets to the user.

Technorati TagsTags:

Comments | RSS

Comments are now closed, but you can still have a jolly time reading what others have left:

#1

Dris said 1871 days ago:

The skin switcher that's built into my (beta) publishing system, Uberblog < http://dris.webhop.org/uberblog.html >, does something similar.

It allows you to specify an entire header and footer for your HTML, depending on the selected skin. This may be a little extreme for some, but I like having maximum flexibility, as well as the knowledge that I can do a redesign at any point in time without worrying about the current markup. In a perfect world (where CSS3 was already a recommendation and all browsers perfectly supported the content generation module), this wouldn't be needed. But, it's not yet a perfect world.
#2

Dysfunksional.Monkey said 1871 days ago:

Here's a quick tip: When using strings within PHP and you're not parsing the string for variables use single quotes.

For the following:
<?php
echo "This is just a load of text";
?>

PHP is looking through the line for a variable to output; in the same way that:
<?php
$content = "text";
echo "This is just a load of $content";
?>

In using single quotes, PHP treats the whole string as a simple character list.

So:
<?php echo 'This is a load of text'; ?>
Will output:
This is a load of text
And:
<?php
$content = "text";
echo 'This is a load of $content';
?>
Will output:
This is a load of $content

There are two main advantages in doing this; Firstly speed, as PHP doesn't need to parse the string for variables, and secondly you don't have to escape every single double-quote when outputting straight (x)HTML.

What might be a simpler approach to your example is to set a cookie on the user's machine with the name of the css/javascript file in it, then include it upon request:
<?php
echo '<script type="text/javascript" language="javascript" src="' . ( ( isset($_COOKIE['hicksdesignflavor']) ) ? $_COOKIE['hicksdesignflavor'] : $default ) . '"></script>';
?>
Obviously, you'll need to set $default (possibly as a constant: define('DEFAULT','/path/to/file.ext'); ), and ask/warn the user regarding the use of cookies.

That way, you're down to a single line of code for the echo, and they get the flavor they want every time they visit.
#3

Gavin said 1870 days ago:

hiya.

instead of using the cookie option why not try sessions (which i've used for a site i have recently built)

I'm using the one posted on the dicussion from ALA;
http://www.alistapart.com/discuss/phpswitch/3/#c4247
http://www.alistapart.com/discuss/phpswitch/9/#c4227

Love the site by the way.. (i also love the redesign of ALA)
#4

Dysfunksional.Monkey said 1870 days ago:

Sessions only last for the time a user is using the site. You *can* set these to last longer, but this information clogs up the server.

Better to use a cookie - essentially its the client's data anyway, as its their option, so there's no point in using valuable server space when its not needed.
#5

gavin said 1867 days ago:

it depends really....the reason i used sessions is

i wanted the style switcher to work if the user had cookies AND javascript switched off!

If the user accepts cookies, the session ID is stored in a cookie, whose name is pre-determined.
If the user doesn't accept cookies, the session ID is propagated through each script via the query string or form inputs.

Elsewhere

The Rissington Podcast - weekly shenanigans with Jon Oxton

Hicksmade - unique handmade goods by Leigh Hicks width=

love

Brit Pack: A proud member I love Omniweb Coda Segment Publishing I buy my type from Veer The Forgiveness Project