Main Content

Setting your colour theme through PHP

Archive - Originally posted on "The Horse's Mouth" - 2006-12-17 16:10:16 - Graham Ellis

Although we usually think of PHP as a server side language concerned with databases and the driving of applications, it's also of great use in controlling the use's look and feel ... indeed, I've just written a number of examples of how PHP can be used to select a set of colours (themes) for the user. Here's the sort of code that you might find in the PHP page.

<?php if ($_REQUEST[colset] == 1) { ?>
body {color: navy; background: olive;}
.person {}
.toptit {font-size: 140%; color: purple;}
.total {color: maroon;}
<?php } else { ?>
body {color: yellow; background: black;}
.person {}
.toptit {font-size: 120%; color: aqua;}
.total {color: green;}
<?php } ?>


The use of a themed approach rather than providing controls over individual attributes means that the page provider can select appropriate combinations and allow the user to quickly and easily switch between the - the opposite alternative of providing controls on a per-attribute basis may give far greater flexibility, but it also gives rise to a system that it's longwinded for the user to change, and a system in which the user can, potentilly, hit on a totally inappropriate combination.

See - source code and code demonstration

Our new course module also includes a step by step guide to generating separate style sheets through PHP - ideal for a shared-style web site, and vital if you're concerned with flexible coding to meet the UK's DDA (Disability Discrimination Act)

Update - I've added a complete example into the new course module that combines PHP, style themes, cookies and a pulldown menu to change the themes. See here for the source code and here to run the code