Forum Moderators: coopster
if ($myrow[$x]<20) { $color="green";}
I have tons of these staements which basically give a cell a certain colour depending on the value of $myrow[$x]
Anyway - trying to functionize it with :
$green="<20"
if ($myrow[$x]$green) { $color="green";}
But it aint working, can't find the answer is it possible?
the value of green could be ==20, <=20, <20 etc.
Use eval [php.net] to evaluate the expression in the strings.
if (eval("$myrow[$x]$green;")) { $color="green";} I´m not sure though whether that is the best way to achieve what you want. Since I´m not exactly sure what you want either it is really hard to go beyond the advice above.
Andreas
toadhall, andreasfriedrich - thanks for the heads up, looks promising I'll let you know how I get on.
Basicaly I have a loop that color codes a table cell depending on the value, the color will either be red, green or yellow. Then I have 19 subsections to the table each with their rules, ie section 1 green might be <20 while for section 2 green could be >=45 etc.
Hopefully for each section I will just have a function where I can pass the operators and thier values.
suggestions welcome :)
ARRGGHHHH
Can't get eval to work properly the code doesn't seem to notice the operator
<?
function larger($x,$y,$z)
{
if (eval("$x $z $y;"))
{echo "$x is the same as $y";}
echo "<p>$x $z $y<p>";
}
larger(2,2, '==');
?>
Its probbably just syntax or something - dont I just want to eval $z?
In PHP 4, eval() returns NULL unless return() is called in the evaluated code, in which case the value passed to return() is returned.
[php.net...]
So your code should probably read something like this:
if (eval("return($x $z $y);")) Andreas
made a function which seems to work buyt only if I put the echo statment in the last braces and then I get more than 13 results (its in a loop)
function color_code($heading, $green, $amber, $red)
{
global $heading, $x, $code, $light, $color, $myrow;
while ($x < $heading) {
if ($myrow[$x]!="$code"[smilestopper]) {
if ($myrow[$x]!=""[smilestopper]) {
if (eval("return($myrow[$x] $green);"[smilestopper])) { $light="green"; $color[$x]=100;}
if (eval("return($myrow[$x] $amber);"[smilestopper])) { $light="amber"; $color[$x]=50;}
if (eval("return($myrow[$x] $red);"[smilestopper])) { $light="red"; $color[$x]=0;}
}
else { $light="white"; $color[$x]="";}
echo "<td class='$light'>$myrow[$x]%</td>\n";
}
}
}
color_code(13, '<20', '==20', '>25');
What the best way to do this?
get $light and $myrow into an array then echo those results outside of the array?
Or use return;? or is there another better way to do this? I've read php.net and can't seem to find a decent example.