On my site, in an include file, I have an array of variables which control colors and styles. It looks something like this
$colors[\'bgcolor\'] = \"#323c58\";
$colors[\'pxfontsize\'] = \"12px\";
$colors[\'fontcolor\'] = \"#c0c0c0\";
$colors[\'fontsize\'] = \"-1\";
$colors[\'fontface\'] = \"tahoma, helvetica, verdana, arial, sans-serif\";
$colors[\'smallfontcolor\'] = \"#c0c0c0\";
$colors[\'smallfontsize\'] = \"-2\";
and so on, for all the variables I need.
Later on, in a function, I want to iterate through this array replacing the value of the variables with any occurrance of the variables name in the string.
Here is the problematic function:
function getLayoutBit($layoutbitname, $getcomments=1)
{
$the_coder_is_bored = doQueryArray(\"SELECT layouttitle,layoutvalue FROM templates WHERE layoutname=\'$layoutbitname\'\");
extract($the_coder_is_bored);
$colors = $GLOBALS[\'colors\'];
extract($colors);
foreach($colors as $colorname => $colorvalue) {
$the_coder_is_bored = str_replace(\"%$colorname%\", \"$colorvalue\", \"$layoutvalue\");
}
if ($getcomments==1) {
return \"<!-- start $layouttitle -->$the_coder_is_bored<!-- end $layouttitle -->\";
} else {
return $the_coder_is_bored;
}
}
Later on I\'ll fix up this function to take template bits based on certain criterion (the objective being database powered user selectable templates). Sure there are plenty of interesting and robust template systems out there already but I want to understand mine and asking for a hand isn\'t really the same as picking up someone elses software.
When I try and run these I get an error that extract expects an array as it\'s first argument which I believe is. I tried adding the line
GLOBALS $colors;
at the top of the function but it didn\'t help. If anybody has some insight (or code) to offer me I would be undescribably greatful as I have already spent way more time then I should have on this problem. One thing that bears mention is that I used a require call to include the file which has the $colors array in it so I know it is being successfully included.
My other question is about eval because, no matter what I do with it, it generates errors. I could sit on the other end of the room and hum quietly and my eval calls would spontaneously combust... To be specific, I would like to know just what kind of PHP code it expects and what causes expected T_VARIABLEs. Could I put the $colors array example I gave on this page in a database and eval it for it work the same? Is it possible to make it work exactly the same (well, not exactly the same as that would mean withholding variables from my other scripts and being a generally belligerant block of code).
Thank you.