Server-side Starter Tutorial:Themes and Skins - Part 3 of 7

Skinning it - the middle

What we need next is for our page to include some code that will make a decision based on the cookie we've set. For our small example of two possibles value - 1 or 2 - for the cookie, our decision making code needs to do this:

If the cookie value is 1, make the body background colour red.
If the cookie value is 2, make the body background colour green.
If there was no cookie, make the body background colour red anyway.

But before we get to that code, how are we going to incorporate a variable as our page background? Simply enough, by changing the <body> statement on our page.

So instead of:

<body bgcolor="red">

We'll use a little bit of php script to dynamically change that code based so that the body bgcolor is set equal to the php variable $body_color (or any other name you care to use). Syntax for that is:

<body bgcolor=" <?php echo $body_color; ?> " >

Enough trivial stuff!

It's time for a 'real' themes file. Following is an example for a new php file called gettheme.php. This file will take the cookie value and set a few variables to be used on our page (and every other page of our site):

<?php
// default theme
if (!isset($theme))
$theme = "1";

// here are our themes
if (!strcmp($theme,"1"))

{
$body_color = "#ff0000";
}

if (!strcmp($theme,"2"))

{
$body_color = "#00ff00";
}
?>

Looks complicated!

Not really, there's a perfectly logical explanation for all of that code ....

«  previous  |  next  »  

Site Links

 

Starter Tutorials