talksr Posted December 5, 2007 Share Posted December 5, 2007 Is there a code which I can put in my php code which will allow me to change the stylesheet used based on what is in the url. So say I have two stylesheets, style1.css and style2.css Is there some php code which would allow me to load style1 by typing in: http://mywebsite.com/index.php?style=1 and then use style2.css by typing in: http://mywebsite.com/index.php?style=2 :-\ Quote Link to comment Share on other sites More sharing options...
BenInBlack Posted December 5, 2007 Share Posted December 5, 2007 yeah, in the header section of the php file you can do this <?php if(isset($_REQUEST['style']) && $_REQUEST['style'] != '') { echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"http://www.mywebsite.com/css/style".$_REQUEST['style'].".css\" />"; } else { echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"http://www.mywebsite.com/css/base_style.css\" />"; } ?> Quote Link to comment Share on other sites More sharing options...
talksr Posted December 10, 2007 Author Share Posted December 10, 2007 Thanks for the help, but I can't get it to work This is my code: <head> <title>Page Title</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <link href="style.css" rel="stylesheet" type="text/css"> <link href="style2.css" rel="stylesheet" type="text/css"> <?php if(isset($_REQUEST['style']) && $_REQUEST['style'] != '') { echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"http://www.mywebsite.com/css/style".$_REQUEST['style'].".css\" />"; } else { echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"http://www.mywebsite.com/css/base_style.css\" />"; } ?> </head> When I load the url like this: http://webhost/index.php?style=2 or http://webhost/index.php?style=1 It only loads the stylesheet values for style2.css Quote Link to comment Share on other sites More sharing options...
davestewart Posted December 10, 2007 Share Posted December 10, 2007 Dude, save yourself some typing! <?php $n = $_REQUEST['style']; if($n != ''){ $stylesheet = "style{$n}.css"; } ?> <head> <title>Page Title</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <link href="base_style.css" rel="stylesheet" type="text/css"> <link href="<?php echo $stylesheet; ?>" rel="stylesheet" type="text/css"> </head> Quote Link to comment Share on other sites More sharing options...
talksr Posted December 10, 2007 Author Share Posted December 10, 2007 Thanks for your post, it works well. Could you explain to me how it works? :-\ Like whats going on on these lines.. <?php $n = $_REQUEST['style']; if($n != ''){ $stylesheet = "style{$n}.css"; } ?> And on these lines: <link href="base_style.css" rel="stylesheet" type="text/css"> <link href="<?php echo $stylesheet; ?>" rel="stylesheet" type="text/css"> I am guessing it inserts which ever stylesheet is in the url onto the page so it can be used. Quote Link to comment Share on other sites More sharing options...
davestewart Posted December 10, 2007 Share Posted December 10, 2007 OK then... $n = $_REQUEST['style']; The variable $n is used to store what we expect in this case, a number. You could use any value that you find easy to remember, but I find $n nice for single numbers, wheras you might use $i in a loop, or $x or $y for coordinates. if($n != ''){ if the value of $n is not an empty string... $stylesheet = "style{$n}.css"; Set the new variable $stylesheet. You can use $dollarVariables inside strings with double-quotes (as opposed to single quotes ' ) and the value of that variable is automatically inserted, as if you had gone "style" . $n . ".css" . In this case I wanted to indicate explicitly to PHP that the variable is $n by using curly braces to separate it from the surrounding text. I didn't HAVE to do this here, but in cases where your variable might run up to some other text it's good practice. Otherwise PHP could get confused, your variable might be "stylesheet$nenglish" as opposed to "stylesheet{$n}english". I tend to use hyphens in file names, that way you can do something like "stylesheet-$n-english" with no ambiguity link href="<?php echo $stylesheet; ?>" rel="stylesheet" type="text/css"> Basic escaping out of HTML into PHP to echo the variable we have already set. Some basic-ish stuff there. Hope that helps! Quote Link to comment Share on other sites More sharing options...
talksr Posted December 10, 2007 Author Share Posted December 10, 2007 Thanks thats really helpful, thank you for your time. Could you finally just explain what this line does? <link href="base_style.css" rel="stylesheet" type="text/css"> I can't see where base_style comes into it? :-\ Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 10, 2007 Share Posted December 10, 2007 lets talk a realistic solution that isn't going to result in an error. First off make all your style sheets into an array or put them into a mysql table the array would be like [code] <?php $styles = array("Flashy","Edgy","Cool","Warm"); ?> Then to get the style info do something like <?php echo "<select name=\"style\">"; foreach($styles as $key => $value){ echo "<option value=\"".$key."\" >".$value."</option>"; } echo "</select>"; ?> [/code] Now they will send you that via get or post so what you then want to do is verify that it is a legit style they sent you so do something like if(array_key_exists ($_POST['style'],$styles)){ $style = $styles[$_POST['style']]; } else{ //default case $style = "default"; } echo "<link href=\"".$style."\" rel=\"stylesheet\" type=\"text/css\">"; ?> That is a much better version use REQUEST contains get and post together thus you could have injection issues. Quote Link to comment Share on other sites More sharing options...
talksr Posted December 10, 2007 Author Share Posted December 10, 2007 Thanks for your post, it seems a better way but I can't get it to completely work. At the moment, a selector drop down is appearing at the top of the page with the values of my array, but it has a >? at the end of it and I can't see where it has come from and also it doesnt seem to pickup my two stylesheets style1 and style 2, I obviously don't expect it to pick up Cool and Warm. <html> <?php $styles = array("style1","style2","Cool","Warm"); ?> <?php echo "<select name=\"style\">"; foreach($styles as $key => $value){ echo "<option value=\"".$key."\" >".$value."</option>"; } echo "</select>"; if(array_key_exists ($_POST['style'],$styles)){ $style = $styles[$_POST['style']]; } else{ //default case $style = "default"; } echo "<link href=\"".$style."\" rel=\"stylesheet\" type=\"text/css\">"; ?> ?> <head> <title>ServerSide Scripting</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <table width="858" height="347" border="1.5"> <!-- Below are the three collumns of text --> <tr> <th scope="col"> <h1>test </h1></th> <th scope="col"> <h2>test </h2></th> <th scope="col"> <h3>test. </h3></th> </tr> </table> </body> </html> Quote Link to comment Share on other sites More sharing options...
davestewart Posted December 10, 2007 Share Posted December 10, 2007 re: base_style Sorry - BenInBlack had added that; I thought it was part of your question. Quote Link to comment Share on other sites More sharing options...
talksr Posted December 10, 2007 Author Share Posted December 10, 2007 That's ok davestewart, could you explain to me on your idea what this does: Code: <link href="base_style.css" rel="stylesheet" type="text/css"> I can't see where base_style comes into it? Quote Link to comment Share on other sites More sharing options...
talksr Posted December 10, 2007 Author Share Posted December 10, 2007 Ok Davestewart, could you explain what this does in the code you did? <link href="base_style.css" rel="stylesheet" type="text/css"> <link href="<?php echo $stylesheet; ?>" rel="stylesheet" type="text/css"> Where does base_style.css come from? :-\ Quote Link to comment Share on other sites More sharing options...
davestewart Posted December 10, 2007 Share Posted December 10, 2007 Hey. It's only another stylesheet, but you don't need to put it in! I misread one of the earlier posts and thought it was part of your original question. :-\ Quote Link to comment Share on other sites More sharing options...
talksr Posted December 10, 2007 Author Share Posted December 10, 2007 Oh I see, I was baffled as to why it was there! Thanks dave P.S. Nice site you seem very gifted in Animation Quote Link to comment Share on other sites More sharing options...
davestewart Posted December 11, 2007 Share Posted December 11, 2007 Hey, you're welcome talksr, and thanks for the compliments! Quote Link to comment 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.