Jump to content

[SOLVED] php css stylesheet url selector


talksr

Recommended Posts

 

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

 

:-\

Link to comment
Share on other sites

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\" />";
}
?>

Link to comment
Share on other sites

Thanks for the help, but I can't get it to work  :P

 

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  :o

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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!

 

 

 

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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?  :-\

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.