So, I’m making a table, and I want to use a 4-color scheme selected at random from a group of 10 schemes.
Here’s my CSS/PHP:
<?php header('Content-type: text/css');
// 0 grey
$color00 = '#959595'; //light grey
$color10 = '#616161'; //medium grey
$color20 = '#414141'; //dark grey
$color30 = '#dfdfdf'; //white grey
// 1 pink
$color01 = '#e45494'; //light pink
$color11 = '#d70060'; //medium pink
$color21 = '#900040'; //dark pink
$color31 = '#f7ccdf'; //white pink
// 2 red
$color02 = '#ee576f'; //light red
$color12 = '#e50428'; //medium red
$color22 = '#9a031b'; //dark red
$color32 = '#facdd4'; //white red
// 3 orange
//(etc, up to 09-39)
$seed = rand(0,9);
$lightcolor = "color0".$seed;
$medcolor = "color1".$seed;
$darkcolor = "color2".$seed;
$whitecolor = "color3".$seed;
?>
#wttbl{
border:0;
border-collapse:collapse;
cellpadding:3;
width:500px;
font-family:Verdana;
font-size:9px;
}
#wttbl td, #wttbl th{
padding: 4px;
}
#wttbl #header{
background:<?="$lightcolor"?>;
}
#wttbl td{
text-align:right;
color:#414141;
}
#wttbl .odd{
background:<?="$color33"?>;
}
#wttbl .even{
}
#wttbl tr:hover{
background:<?="$color03"?>;
}
#wttbl td:hover{
background:<?="$color23"?>;
color:white;
}
The problem is that, hardcoding things like
#wttbl .odd{ background:<?="$color33"?>; }
seem to work, and echo the proper css color into my code, while things like
#wttbl #header{ background:<?="$lightcolor"?>; }
echo “color03” into the tag.
What am I doing wrong, and how can I fix it?
You can enclose a string inside
{}to reference a variable by the string that makes up its name:This does not seem like the best method, however. It would be more appropriate to store them in arrays. Building variable names (called variable variables in PHP) tend to become difficult to debug, especially in the global namespace. It becomes hard to keep track of what variables have been defined, or how many have been defined.