我在表中有行,我想根据值更改某些元素的类。我现在拥有的:
<td><span class="label label-warning">".$rows["riskScoreText"]."</span></td>
我将如何做到这一点?
答案 0 :(得分:3)
试试这个:
$value = $rows["riskScoreText"];
if($value == 'High') {
$class = "label label-warning";
} else {
$class = "label label-low"; }
<td><span class="<?php echo $class>">".$rows["riskScoreText"]."</span></td>
当然,如果您有两个以上的类,可以使用elseif语句添加它们。
答案 1 :(得分:1)
基于额外的引号,我假设你已将其从现有的回声中拉出来。所以它看起来像:
echo '<td><span class="label ';
if($rows["riskScoreText"] == 'High'){ echo "label-warning"; }
elseif($rows["riskScoreText"] == 'Low'){ echo "label-success"; }
echo '">'.$rows["riskScoreText"].'</span></td>';
答案 2 :(得分:0)
假设您正在运行循环
<?php
foreach( $array as $rows ) {
if( $rows["riskScoreText"] == "High" ) {
$class = " label-warning";
} elseif( $rows["riskScoreText"] == "Low" ) {
$class = " label-success";
} else {
$class = ""; //NO STYLE?
}
?>
<td><span class="label<?php echo $class; ?>"><?php echo $rows["riskScoreText"]; ?></span></td>
<?php
}
?>