iconicCreator Posted June 18, 2009 Share Posted June 18, 2009 I have two style sheets, one I'm going to call zoom.css and the other normal.css I need to have a php script that can process the switching. I also need a button that the user can click to initiate the switching. I have searched Google and there are many but being a novice, the problem is I don't know which is better or secure. Some of them lack additional explanation that will help a PHP novice use their script or example. There are also lots of conflicting posting in their comments/blog that just further confuses me. Does anyone know of any tutorials or resources that I can use to get this going? Thanks very much everyone! IC Quote Link to comment https://forums.phpfreaks.com/topic/162786-using-php-to-switch-different-style-sheets/ Share on other sites More sharing options...
jsschmitt Posted June 18, 2009 Share Posted June 18, 2009 Are you looking to change the CSS for the entire page? Or just a section? Quote Link to comment https://forums.phpfreaks.com/topic/162786-using-php-to-switch-different-style-sheets/#findComment-859031 Share on other sites More sharing options...
J.Daniels Posted June 18, 2009 Share Posted June 18, 2009 PHP is a server side language, which means it cannot change anything after the page has loaded without a reload. With that being said, you can choose which stylesheet is used when you output the html. For example: <html> <head> <?php $css = 'zoom'; if ($css == 'zoom') { echo '<link type="text/css" rel="stylesheet" media="screen" href="zoom.css">'; } else { echo '<link type="text/css" rel="stylesheet" media="screen" href="normal.css">'; } ?> </head> <body> If you want a button to change the stylesheet, the page would reload and you would set $css to whatever you want. To change the stylesheet after the page has been loaded, you would have to use Javascript. Quote Link to comment https://forums.phpfreaks.com/topic/162786-using-php-to-switch-different-style-sheets/#findComment-859033 Share on other sites More sharing options...
wildteen88 Posted June 18, 2009 Share Posted June 18, 2009 You can accomplish this with some simple code. What I'd do is create a new PHP script called style.php. When linking your stylesheet you'd do use sstyle.php as the stylesheet <link rel="stylesheet" type="text/css" href="styles.php"> Now in styles.php you serve up the requested stylesheet <?php header('Content-type: text/css'); // list all available styles in an array $stylesheets = array('zoom', 'normal'); // check that a request to change the style sheet has been made, and that it is a valid style sheet if(isset($_GET['style']) && in_array($_GET['style'], $stylesheets)) { $stylesheet = $_GET['style']; // set a cookie to remember new style setcookie('style', $stylesheet, time()+3600); } // check to see if a cookie is set elseif(isset($_COOKIE['style'])) { $stylesheet = $_COOKIE['style']; } // no request or cookie set default stylesheet as normal. else $stylesheet = 'normal'; // now all the hard work has been done load the requested stylesheet include $stylesheet . '.css'; ?> Now to change style on any page you just need to use the following url yoursite.com/somepage.php?style=zoom or yoursite.com/somepage.php?style=normal Quote Link to comment https://forums.phpfreaks.com/topic/162786-using-php-to-switch-different-style-sheets/#findComment-859034 Share on other sites More sharing options...
jsschmitt Posted June 18, 2009 Share Posted June 18, 2009 Since we aren't waiting for an answer about what css he is looking to change.. Put in <head> <?php $css = $_POST['css']; ?> <link rel="stylesheet" type="text/css" href="<?php echo $css.'css'; ?>"> Put in <body> <FORM action='$_SERVER['PHP_SELF']' method='post'> <?php if ($css=="zoom"){ echo "<input type='submit' name='css' value='normal' />"; else echo "<input type='submit' name='css' value='zoom' />"; }; ?> </form> Quote Link to comment https://forums.phpfreaks.com/topic/162786-using-php-to-switch-different-style-sheets/#findComment-859044 Share on other sites More sharing options...
jsschmitt Posted June 18, 2009 Share Posted June 18, 2009 Since I am retarded when typing... this is what it should look like in a very basic form. For use, see: http://www.anomymage.com/tests/do/css/ <html> <head> <?php if (empty($_POST)) { $css = 'normal';} else{ $css = $_POST['css'];}; ?> <link rel="stylesheet" type="text/css" href="<?php echo $css.'.css'; ?>"> </head> <body> <FORM action='<?php echo $_SERVER['PHP_SELF'] ?>' method='post' id="cssswitcher"> <?php if ($css=="zoom"){ echo "<input type='submit' name='css' value='normal' />";} else{ echo "<input type='submit' name='css' value='zoom' />"; }; ?> </form> <div id="test">Hello World</div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/162786-using-php-to-switch-different-style-sheets/#findComment-859054 Share on other sites More sharing options...
iconicCreator Posted June 18, 2009 Author Share Posted June 18, 2009 Are you looking to change the CSS for the entire page? Or just a section? I'm very sorry, I was away from the computer. Yes, I want to change the entire style sheet. I have two sets of style sheets in their own folders. Example: style_normal.css Example: style_zoom.css I want to assign a button on the page. The default style will be the style_normal.css, this is the style that will be on the page when user visit. And if they decide to zoom the page, they can click the button which will fire up the zoom style sheet. Thanks for all your valueble help, time and patience. IC Quote Link to comment https://forums.phpfreaks.com/topic/162786-using-php-to-switch-different-style-sheets/#findComment-859084 Share on other sites More sharing options...
iconicCreator Posted June 18, 2009 Author Share Posted June 18, 2009 You can accomplish this with some simple code. What I'd do is create a new PHP script called style.php. When linking your stylesheet you'd do use sstyle.php as the stylesheet <link rel="stylesheet" type="text/css" href="styles.php"> Now in styles.php you serve up the requested stylesheet <?php header('Content-type: text/css'); // list all available styles in an array $stylesheets = array('zoom', 'normal'); // check that a request to change the style sheet has been made, and that it is a valid style sheet if(isset($_GET['style']) && in_array($_GET['style'], $stylesheets)) { $stylesheet = $_GET['style']; // set a cookie to remember new style setcookie('style', $stylesheet, time()+3600); } // check to see if a cookie is set elseif(isset($_COOKIE['style'])) { $stylesheet = $_COOKIE['style']; } // no request or cookie set default stylesheet as normal. else $stylesheet = 'normal'; // now all the hard work has been done load the requested stylesheet include $stylesheet . '.css'; ?> Now to change style on any page you just need to use the following url yoursite.com/somepage.php?style=zoom or yoursite.com/somepage.php?style=normal Thanks very much for the help, I'm not on the computer that contained the files that I'm working on for this project. However,I do have some questions. Will it be better to use session instead of cookie, since the user can turn cookies off? Also I see you have a duration in the code, 3600, do you need a duration for the style? With the button, the user can switch between styles. Just trying to learn this stuff. If you can share some light on this. Thanks again. IC Quote Link to comment https://forums.phpfreaks.com/topic/162786-using-php-to-switch-different-style-sheets/#findComment-859144 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.