Jump to content

Stylesheet Switching with PHP.


Recommended Posts

Does anybody know how to create a PHP function wherein one can switch from one stylesheet to another? I have seen many examples of this, and some are written in javascript. The problem is, for example if you go to page 2, the default stylesheet will then be used again. For example i have 3 themes/CSS for my website, RED, BLUE, AND ORANGE. for example, i selected RED theme and then decided to go to another page of the website. I should be maintaining the RED theme. and so if im already in page 2 then i select BLUE theme, wherever i go, i should be maintaining the blue theme not unless i selected a new theme.I have read about Peter-Paul Koch javascript switching technique, but i think it only works for one page. I know it is possible i just dont know how for i am a bit a beginner at php scripting.

Link to post
Share on other sites
Does anybody know how to create a PHP function wherein one can switch from one stylesheet to another? I have seen many examples of this, and some are written in javascript. The problem is, for example if you go to page 2, the default stylesheet will then be used again.

Yeah, I've had this same problem myself earlier, and didn't know any php. I still don't know much really. I just made a javascript that redirected me.
Choose a theme!	<p>	<object>	<form name="form" action="">		<select name="site">		<option class="choose" value="">Choose theme...</option>		<option value="<!--www.yourdomain.com-->/index.html">Blue</option> <!-- Default page and theme -->	      	<option value="<!--http://www.yourdomain.com-->/green/index.html">Green</option> <!-- Green page -->	      	<option value="<!--http://www.yourdomain.com-->/orange/index.html">Orange</option> <!-- Orange page -->	      	<option value="<!--http://www.yourdomain.com-->/red/index.htm">Red</option> <!-- Red page -->		</select>	<br /><br />	<input type="button" value="Change theme!" onclick="javascript:formHandler(this)" />	</form>	</object>	</p>

Of course, duplicating every document for three or four colors is not very practical for a large page... :)

Link to post
Share on other sites

That's the main problem. What if your site is composed of 1000 pages? You dont really want to duplicate it all. You just need a PHP function that will echo the preferred CSS file in the head. It would really save you a lot of webspace. Ive read somewhere that i should use caches but i dont really know how to do it yet. Im still working it out. Hope somebody can help.

Link to post
Share on other sites

Do something like this:

<link rel="stylesheet" href="<?php echo $_POST['sheet']; ?>" type="text/css" />.

Where the 'sheet' is the name of the select tag in your form menu as well as the sheet file name, and POST the form method you used (can be changed to GET).

Link to post
Share on other sites

My friend was just asking about his... I can gety a copy from him laterWell you really need 2 pagessetting.php & setc.phpto change the style sheet people will go to settings.php... check the one they want then click SAVE.save will send them to setc.php with the value for the cookie. setc.php will hen set a cookie telling the pages to use what style sheet.Then when they go to your site, every page will look for the cookie, then look at the value. then set the style sheet.______________________________________________________________________setting.php:<form action="setc.php" method="post"><div align="left"><input type="radio" name="style" value="style.css" checked> Defult style setting<br></div><div align="left"><input type="radio" name="style" value="style2.css">Style 2<br></div><div align="left"><input type="radio" name="style" value="style3.css">Style 3<br></div><div align="center"><input type="submit" value="submit"></form>setc.php<?php$style = $_POST['style'];if($style != ""){ setcookie( "sitestyle", "$style", time()+31536000, "/" );} else{ setcookie( "sitestyle", "", time()+31536000, "/" );}echo("<script>location = '/index.php';</script>");?>ALL PAGES<?php $style = $_COOKIE['sfb'];if(!$_COOKIE['sitestyle']) { echo "<link rel='stylesheet' type='text/css' href='/style.css' />"; } else { echo "<link rel='stylesheet' type='text/css' href='/$style' />"; } ?>(this may have bugs in it but you get the main idea)

Link to post
Share on other sites

i get the idea... i really appreciate the help. but one thing more... what if i only use one page? and added 3 links at the top of each page for stylesheet switching? then after i click on the link the page would simply reload itself including the new stylesheet. hope im not asking too much. but thank you! :)

Link to post
Share on other sites

Fairly simple to do that last one :) :)learn how to use $_GET["your_variable"], use it on the targets of the links, to the same page.Then ask in PHP for that variable. If a..Ahwel,, I can't explain. Look at this:

<html><head><title>some</title><?phpswitch (@$_GET["Skin"]) {case "redSkin":echo '<link type="text/css" rel="stylesheet" href="red.css" />';break;case "blueSkin":echo '<link type="text/css" rel="stylesheet" href="blue.css" />';break;}?></head><body><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?Skin=redSkin">Go Red!</a><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?Skin=blueSkin">Go Blue!</a>...
Don't know if it works though :(
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...