body
{
background-color: #000000;
}
Basically all that style does is change the background of the page to black. Not very impressive, though adequate for our needs. If we were to save this as say 'style.php' and try calling it using the following command;
<link type="text/css" rel="stylesheet" href="style.php">
We wouldn;t get a style that loaded. Basically this is because the output of this .php file is not formatted to .css. TO over come this we just need to include the following code at the beginning of the code, before there is any information outputted;
Header ("Content-type: text/css");
This basically formats the output so that it is accepted as a .css file. So the full code for our example would be as follows;
<?
Header (Content-type: text/css");
?>
body
{
background-color: #000000;
}
I'm sure your asking yourself what is the use of this. We it has the following uses. For example say that you had multiple themes. You could store the values in either conditional statements or a mult-dimensional array, such as the following;
<?
Header ("Content-type: text/css");
$array = array("black" => array("background" => "#000000", "textColor" => "#FFFFFF"), "white" => array("background" => "#FFFFFF", "textColor" => "#000000"));
?>
body
{
background-color: #000000;
}
This array basically just allows us to change the background and texts colors. However at the moment it is still a static file. Now what if instead of calling style.php we were to call style.php?theme=white and then use $_GET['theme'} in our code. The result would probably be similiar to below;
<?
Header ("Content-type: text/css");
$theme = $_GET['theme'];
$array = array("black" => array("background" => "#000000", "textColor" => "#FFFFFF"), "white" => array("background" => "#FFFFFF", "textColor" => "#000000"));
?>
body
{
background-color: <? echo $array[$theme]['background']; ?>;
}
Now this allows us to change what our theme is depending on what the value of theme is that we call. Now that might be interestsing, though it isn;t very helpful by itself. However consider the following scenario. Say you have a link on your page that allows the user to change the theme. Say this link takes you to a page where it sees what selection you picked and then assigns a keyword identifying this theme to a session. Then imagine that this page then takes you back to our original page where a piece of code then sees if there is a session present and if there is it calls the right theme, and if not it calls a default theme. This might look like the following three files;
styles.php
<?
Header ("Content-type: text/css");
$theme = $_GET['theme'];
$array = array("black" => array("background" => "#000000", "textColor" => "#FFFFFF"), "white" => array("background" => "#FFFFFF", "textColor" => "#000000"));
?>
body
{
background-color: <? echo $array[$theme]['background']; ?>;
}
index.php
<?
session_start();
//Checks to see if there is a theme selected, if so use that theme otherwise use te default theme
if($_SESSION['theme']){
$theme = $_SESSION['theme'];
}else{
$theme = "black";
}
//This is the code that allows the theme selection to return back to this page
$url = $_SEREVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
?>
<html>
<head>
<link type="text/css" rel="stylesheet" href="style.php?theme<? echo $theme; ?>">
</head>
<body>
<a href="select.php?theme=black&url=<? echo $url; ?>"Black Theme</a>
<a href="select.php?theme=white&url=<? echo $url; ?>"White Theme</a>
</body>
</html>
select.php
<?
session_start();
//Assign theme selection to session
$_SESSION['theme'] = $_GET['theme'];
//Return to previous page
$return = "Location: " . $_GET['url'];
return ($return);
?>
Now this code should give you a basic theme selection. This can be expanded, to say allow users to have a semi-permanent selection through the use of cookies, or maybe even allowing users to create and use their own custom themes.
Well thats all I hope you enjoyed this article, and that it was helpful.
discuss this topic to forum
