Web Development

How to Make a Drop Down Template Theme Selector with PHP

How to Make a Drop Down Tempalte Theme Selector

This tutorial will help teach you how to make your site have the option to allow the users to choose the look and feel of the site using PHP, and MySQL wrapped around a HTML template.

To start off create a MySQL database called theme.  To learn how to do this go to http://www.microcyb.com/?m=c&c=437 and then submit the following SQL into the database called theme.


INSERT INTO template VALUES (1, './template1.html', 'First Template');
INSERT INTO template VALUES (2, './template2.html', 'Second Template');

Now we are going to have multiple files, so create a new folder on your system.  After you have created a new folder on your system, make a simple text document and rename it to settings.php

settings.php (copy and paste the following informtion into settings.php)

<?
# Root MySQL database
$base = "theme";
# MySQL user name
$user = "";
# MySQL password
$password = "";
# Host name (ex. localhost)
$host = "localhost";
$cms_template= "template";
# -----------------------------------------
# CHOOSE THE DEFAULT TEMPLATE IF NO COOKIE
# -----------------------------------------
$default_theme = "./template1.html";
?>

Create a text document and rename the file to index.php

index.php (copy and paste the following informtion into index.php)


http://www.microcyb.com   */
/* ------------------------------------------------------------ */
# -----------------------------------------------------------------------------
# START OF NEW CODE TO CALL THE SETTINGS AND START THE MYSQL CONNECTION
# -----------------------------------------------------------------------------
include_once ("./settings.php");
$link = mysql_connect($host,$user,$password);
mysql_select_db("$base");
# -----------------------------------------------------------------------------
# START OF NEW CODE TO CALL THE TEMPLATE IF NONE IS SELECTED
# -----------------------------------------------------------------------------
if (!$filename)
 {
 $filename=''.$default_theme.''.$template.'';
 }
if(!$fd = @fopen($filename, "r"))  
 {
 session_start();
 $filename="$default_theme";;
 setCookie ("filename","", time()+30240000);
 }
# -----------------------------------------------------------------------------
# START OF NEW CODE TO CALL THE TEMPLATE IF NONE IS SELECTED
# -----------------------------------------------------------------------------
$theme .= '<form METHOD="POST" style="word-spacing: 0; margin: 0">
<select name="list" onChange="showpage(this.form);" class="input">
<option value="x">--------------</option>\n';
$result = mysql_query("SELECT * FROM $cms_template") or die ("Can't execute query.");
while(($row = mysql_fetch_object($result)))
  {
$theme .= '<option value="'.$row->filename.'">'.$row->name.'</option>\n';
  }
$theme .= '</select></form>
<script language="JavaScript">
function showpage(form)
{
document.cookie = "filename=\'\'";
var expireDate = new Date;
expireDate.setMonth(expireDate.getMonth()+6);
document.cookie = "filename=" + document.all.list.value + "; expires=" + expireDate.toGMTString();
parent.document.location=parent.document.location;
};
</script>';
# -----------------------------------------------------------------------------
# SET THE VARIBLE $SITE_MAIN TO HAVE INFORMATION
# -----------------------------------------------------------------------------
$site_main ="<b>Hello</b>, Microcyb here!<br>This is an exmaple of changing the look of your site!";
# -----------------------------------------------------------------------------
# NOW WE WRAP THE DATA AROUND AN HTML TEMPLATE
# -----------------------------------------------------------------------------
$template = fread ($fd, filesize ($filename));
fclose ($fd);
$template = stripslashes($template);
$template = eregi_replace("{main}", "$site_main", $template);
$template = eregi_replace("{theme}", "$theme", $template);
echo "$template";
?>

Create a text document and rename the file to template1.html

template1.html (copy and paste the following informtion into template1.html)



About the author

Written by Darren W..

Darren Hedlund is a freelance Web developer, writer, and data analyst. Darren has a degree in Computer Information Science and has spent the last 15 years developing application and environments from hand held, windows, web, virtual science, gaming, artificial intelligence and graphics design.

Darren's coding knowledge ranges from C+, Visual Basic, .NET, PHP, JSP, REXX, KIXX, and many others. His graphical and environmental knowledge stems in Macromedia Flash, 3D studio Max, Curious Labs Poser, Adobe Photoshop, and many others. Darren works in many platforms ranging from database, visual design, and, system development.

If you found this post useful you may also want to check these out:

  1. How to make your own PHP template script
  2. Creating a Drop Down Selection with an Array
  3. Template Driven PHP Architecture
  4. Replacing Text in a MySQL Database Using PHP
  5. How to make a Hit Counter with PHP
  6. Using Sessions to Make a PHP Login Script