Jump to content

Recommended Posts

Hi there, i want to have an option for a user to change colors/themes on my page... it works fine, but it's just running really slowwww... i was wondering if there was a way to optimize this code??

i'm using a GET on the same page (ie: if they click the orange color it sends to (pgg.php?c=o), etc.) like i said it works, just wondering if there was anything i could do to make it run faster? i know i could use switches on the if statements but that wouldn't slow it up that much would it?

<?php
$theme = $_GET["c"];
if (!isset($theme)){
echo '<link rel="stylesheet" type="text/css" href="default.css" media="screen"/>';
}
if ($theme == "g"){
echo '<link rel="stylesheet" type="text/css" href="default.css" media="screen"/>';
}
if ($theme == "b"){
echo '<link rel="stylesheet" type="text/css" href="blue.css" media="screen"/>';
}
if ($theme == "p"){
echo '<link rel="stylesheet" type="text/css" href="pink.css" media="screen"/>';
}
if ($theme == "o"){
echo '<link rel="stylesheet" type="text/css" href="orange.css" media="screen"/>';
}

?>

<title>CLB 411 - Search</title>
<style type="text/css">
<!--
.style2 { font-size: 1.2em;
color: #FFFFFF;
}

-->
</style>
[quote] <?php
$theme = $_GET["c"];
if (!isset($theme)){
echo '<link rel="stylesheet" media="all" type="text/css" href="final_drop.css" />';
}
if ($theme == "g"){
echo '<link rel="stylesheet" media="all" type="text/css" href="final_drop.css" />';
}
if ($theme == "b"){
echo '<link rel="stylesheet" media="all" type="text/css" href="blue_drop.css" />';
}
if ($theme == "p"){
echo '<link rel="stylesheet" media="all" type="text/css" href="pink_drop.css" />';
}
if ($theme == "o"){
echo '<link rel="stylesheet" media="all" type="text/css" href="orange_drop.css" />';
}
?>[/quote]
Link to comment
https://forums.phpfreaks.com/topic/31614-color-changing-options-switching-csss/
Share on other sites

I personally would do something like:
[code]switch ($_GET["style"])
{
case "1":
$stylesheet = "style1.css";
case "2":
$stylesheet = "style2.css";
default:
$stylesheet = "style1.css";
}

echo '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" media="screen"/>';[/code]
Thanks for the help, question about the code though....

i dunno if i'm doing this right because it's not passing the css right...

[quote]switch ($_GET["c"])
{
case "g":
$theme = "default.css";
case "b":
$theme = "blue.css";
case "p":
$theme = "pink.css";
case "o":
$theme = "orange.css";
default:
$theme = "default.css";
}

echo '<link rel="stylesheet" type="text/css" href="'.$theme.'" media="screen"/>';[/quote]

I want it to be equivelant to this code...

[quote]$theme = $_GET["c"];

if (!isset($theme)){
echo '<link rel="stylesheet" type="text/css" href="default.css" media="screen"/>';
}
if ($theme == "g"){
echo '<link rel="stylesheet" type="text/css" href="default.css" media="screen"/>';
}
if ($theme == "b"){
echo '<link rel="stylesheet" type="text/css" href="blue.css" media="screen"/>';
}
if ($theme == "p"){
echo '<link rel="stylesheet" type="text/css" href="pink.css" media="screen"/>';
}
if ($theme == "o"){
echo '<link rel="stylesheet" type="text/css" href="orange.css" media="screen"/>';
}[/quote]
You need the breaks in the switch:
[code]<?php
switch ($_GET["c"])
{
case "g":
$theme = "default.css";
break;
case "b":
$theme = "blue.css";
break;
case "p":
$theme = "pink.css";
break;
case "o":
$theme = "orange.css";
break;
default:
$theme = "default.css";
break;
}

echo '<link rel="stylesheet" type="text/css" href="'.$theme.'" media="screen"/>';
?>[/code]
You need to use a break after each case. eg;

[code]
<?php
switch ($_GET["c"]) {
  case "g":
    $theme = "default.css";
    break;
  case "b":
    $theme = "blue.css";
    break;
  case "p":
    $theme = "pink.css";
    break;
  case "o":
    $theme = "orange.css";
    break;
  default:
    $theme = "default.css";
    break;
}
?>
[/code]
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.