PHP if statments is some of the first a beginner learns about, there is a useful PHP Tutorial at Brugbart.
Today i’m going to make a php if then else statement, to chose different CSS settings, based on choices the user makes. PHP is supported on most shared hosts, so its a good choice when developing your website.
To create a stylesheet selector, we can use the below code, provided by Brugbart.
<style type=”text/css”>
<?php session_start();
if ((isset($_GET['StyleSheet'])) && (preg_match(“/^[\d]{1,2}$/D”, $_GET['StyleSheet']))) {
$_SESSION['styl'] = $_GET['StyleSheet']; } else { // Default StyleSheet
$_SESSION['styl'] = ’1′; }
echo ‘@import url(“StyleSheet’ . $_SESSION['styl'] . ‘.css”);’;
?>
</style>
This allow me to select another style, simply by clicking a static HTML link. See below.
<a href=”?StyleSheet=1″>StyleSheet 1</a>
This technique can also be used to change single styles, such as the font-size as we are using the selector in the head section of out page.
<style type=”text/css”>
<?php session_start();
if ((isset($_GET['StyleSheet'])) && (preg_match(“/^[\d]{1,2}$/D”, $_GET['StyleSheet']))) {
$_SESSION['styl'] = $_GET['StyleSheet']; } else { // Default StyleSheet
$_SESSION['styl'] = ’1′;
if ($_SESSION['styl'] == ’1′) { echo ‘p { font-size: 2em; }’; }
elseif ($_SESSION['styl'] == ’1′) { echo ‘p { font-size: 4em; }’; }
else { echo ‘p { font-size: 4em; }’; }
}
?>
</style>