基于变量值的条件PHP样式


Conditional PHP styling based on variable value

我有一些PHP代码,执行并从SQL表中选择10行。其中一列result可以保存赢或输的值。

if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo "<div class = 'logrow'> <img src ='". $row["url"] ."'</img> <p class = 'logtext'>". $row["name"] ." bet $". $row["amount"] ." with a ".  $row["chance"] ."% chance and ". $row["result"] .". </p> </div>";
    }

我怎么能做一些事情,这将从每行的值回显如上,但回显不同的语句为每行,其中result的值是损失。例如,为背景颜色添加内联样式。

例如,假设我找到了10行-其中9行是$row["result"],所以它们应该如上所述。但有一行的$row["result"]值为损耗,则应采用不同的回波。可以使用内联样式,也可以插入一个保持这种样式的变量。

我知道这是非常具体的,可能不清楚,所以提前感谢。

根据$row["result"](赢/输)中的值创建一个类并在echo

中使用它
css:
    .won{background-color::blue}
    .loss{background-color::red}
if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo "<div class = 'logrow ". $row["result"] ."'> <img src ='". $row["url"] ."'</img> <p class = 'logtext'>". $row["name"] ." bet $". $row["amount"] ." with a ".  $row["chance"] ."% chance and ". $row["result"] .". </p> </div>";
    }

尝试更改类名。

<?php 
 $status = $row['result'];
 $classname = 'won';
 if($status == 0){
   $classname = 'fail';
 }
?>
<div class = 'logrow <?php echo $classname ?> '> <img src ='". $row["url"] ."'</img> <p class = 'logtext'>". $row["name"] ." bet $". $row["amount"] ." with a ".  $row["chance"] ."% chance and ". $row["result"] .". </p> </div>";

现在在样式表下为wonfail定义类。

<style type="text/css">
.won{ color: green; }
.fail{ color: red; }
</style>