-3

I've stored values in an array and I want to apply conditional formatting to the output when I access them. I want the text of the values, less than or equal to 10, displayed in red or, greater than 10, displayed in green.

For example, below, I would like the resultant value of 5 to be displayed in red.

I'm a newbie to this but would assume I would need the help of CSS to achieve this.

Any advice would be appreciated.

<?php

$total_sql=array();
$total_sql[]=5;
$total_sql[]=10;
$total_sql[]=15;
$total_sql[]=20;

print($total_sql[0]);

?>
4

2 に答える 2

1
<?php

$total_sql=array();
$total_sql[]=5;
$total_sql[]=10;
$total_sql[]=15;
$total_sql[]=20;

print($total_sql[0]);

?>

foreach($total_sql as $item)
{
if($item >10)
{
    echo '<p class="green">'.$item.'</p>';
} else {
    echo '<p class="red">'.$item.'</p>';
}


.red{color:red;}
.green{color:green}
于 2012-11-07T13:18:14.443 に答える
0

One liner:

echo '<div style="color: '.(($score == '5') ? 'red' : 'black').';">'.$score.'</div>';

Comparison operator could be:

$score == 'x'
        > 'y'
       >= 'z'
etc...

I've used an inline style here, but you can use class= too.

于 2012-11-07T13:42:27.717 に答える