Switching Themes with PHP |
Switching Themes with PHP |
![]()
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. |
|
|
![]() |
*mipadi* |
![]()
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.) |
|
|
![]() ![]() |