Log In · Register

 
Switching Themes with PHP
Fabio.
post Dec 5 2005, 03:17 PM
Post #1


^ Mrs. Jonas
*****

Group: Member
Posts: 592
Joined: Apr 2005
Member No: 263,313



The closest I got when searching was this, and it didn't help.

I'm working on another website, and want to be able to switch themes, and like Heather had said, switch it on all pages. It's NOT wordpress, by the way.

I have header.inc and footer.inc, and then index.php which includes both. I know that I can make header2.inc and footer2.inc, but I don't know how to make some sort of dropdown where you can choose whether you want theme 1 or theme 2, and how to make it include the appropriate header and footer.

I tried googling it, but only came up with Wordpress and B2 hacks, which isn't helping me at all.
 
 
Start new topic
Replies
*mipadi*
post Dec 5 2005, 03:48 PM
Post #2





Guest






Are you doing it for a general website? It's fairly simple, assuming I am following you correctly. It involves three things: making a separate stylesheet for each "theme", making a form to select themes, and write code to handle that.

First, the form: Just make a normal HTML form. The code will look something like this:
CODE
<form method="get" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>
   <select name="style">
       <option value="stylea">A</option>
       <option value="styleb">B</option>
   </select>
   <input type="submit" value="Submit"/>
</form>[/code
Put this wherever you want the form to appear. Naturally, you fill in an <option> tag for each option. Feel free to customize the look of the form, as well as the label for the Submit button (in the "value" attribute").

Now, I'm assuming your header.inc file includes the stylesheet, with a line such as:
[code]<link rel="stylesheet" type="text/css" href="style.css"/>

(<?php echo $_SERVER['SCRIPT_NAME']; ?> inserts the filename of the page.)

What you need to do is grab the value from the form, and insert it into HTML code. Assume for a second your stylesheets are named according to the values you used in your form (i.e., you have a style called "stylea.css" and a style called "styleb.css"). When you select a new style, your page will change from, say, page.php to page.php?style=stylea. "style", of course, contains the value of the stylesheet you selected.

Your coding in your header file would look something like this:
CODE
<html>
<head>
   <title>blah blah</title>
   <?php echo '<link rel="stylesheet" type="text/css" href="'.$_GET['style'].'"/>'; ?>
   <!-- rest of code here -->

<?php echo '<link rel="stylesheet" type="text/css" href="'.$_GET['style'].'"/>'; ?> inserts the appropriate <link> tag, plus the value of the "style" property in the query string (the thing after the ? in the URL, in this case, ?style=styla).

Now, whenever the user selects a new theme, the appropriate stylesheet will be included.




(Also, as a minor side note, it is more secure to name include files as "include.php" rather than "include.inc". If a user navigates directly to the file (such as by typing in host.com/include.inc, they will see all the source code for the file--which is had if the source code contains passwords to databases or other confidential data. However, if a user navigates to an include file at host.com/include.php, the include file will be executed and the user will only see output that is explicity shown using PHP.)
 

Posts in this topic
Fabio.   Switching Themes with PHP   Dec 5 2005, 03:17 PM
mipadi   Are you doing it for a general website? It's f...   Dec 5 2005, 03:48 PM
Fabio.   I think that's what I'm looking for. Let m...   Dec 5 2005, 07:25 PM
Fabio.   -sorry, double post-   Dec 5 2005, 07:25 PM
insomniac   are you looking for this: http://codegrrl.com/tuto...   Dec 5 2005, 09:08 PM
Fabio.   QUOTE(insomniac @ Dec 5 2005, 9:08 PM)are you...   Dec 6 2005, 07:19 PM
mipadi   QUOTE(Fabio. @ Dec 6 2005, 7:19 PM)DOES it af...   Dec 6 2005, 07:26 PM
Heathasm   oh, mel, i bookmarked the site i got my code from...   Dec 6 2005, 04:12 AM
insomniac   QUOTE(Heathasm @ Dec 6 2005, 4:12 AM)oh, mel,...   Dec 6 2005, 09:15 AM
toyo loco   Just be aware that (well according to what people ...   Dec 6 2005, 10:16 AM
Heathasm   no, not at all its just a few lines of php and ext...   Dec 6 2005, 01:39 PM
insomniac   i dont want to steal lisa's bandwidth.. i ...   Dec 6 2005, 05:36 PM
toyo loco   ^ you should ask who knows her bandwith might not ...   Dec 6 2005, 07:09 PM
insomniac   how would you do this with wordpress? .... im n...   Dec 6 2005, 08:37 PM
Fabio.   http://codegrrl.com/tutorials/php/skinning_your_si...   Dec 6 2005, 09:04 PM
insomniac   ^you use wordpress right? how are you doing it?   Dec 6 2005, 09:55 PM
Fabio.   Well, I haven't adapted it to work with wordpr...   Dec 6 2005, 10:48 PM
mipadi   QUOTE(Fabio. @ Dec 6 2005, 10:48 PM)Here...   Dec 6 2005, 10:54 PM
Fabio.   QUOTE(mipadi @ Dec 6 2005, 10:54 PM)You have ...   Dec 6 2005, 11:06 PM
insomniac   QUOTE(Fabio. @ Dec 6 2005, 11:06 PM)Edit: Ahh...   Dec 7 2005, 12:00 AM
insomniac   michael, do you know how to make it work with word...   Dec 6 2005, 10:56 PM
mipadi   QUOTE(insomniac @ Dec 6 2005, 10:56 PM)michae...   Dec 6 2005, 11:00 PM


Closed TopicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members: