在css类中使用echo


Use echo in a css class

我在项目的不同页面上使用php echo来更新每天都在变化的百分比值。此值可以是负数,也可以是正数。

这是我的代码:

<i class="icon-thumbs-up"><strong> <?php $file = file('include.txt');echo $file[n]; ?></strong></i>

我正在使用FontAwesome图标与引导模板。一切都很顺利。

现在,如果百分比为负数,我想使用class="icon thumbs down"而不是class="icon-thumbs up"。

我已经尝试使用来实现这一点

<i class="<?php $file = file('include.txt');echo $file[n]; ?>"><strong> <?php $file = file('include.txt');echo $file[13]; ?></strong></i>

以便在所有页面上进行更改。

然而,这并不奏效。谢谢你的提示!

澄清:

<i class="icon-thumbs-up"><strong> <?php $file = file('include.txt');echo $file[0]; ?></strong></i>

include.text的内容:第1行0.58%->一切正常。我竖起大拇指显示,旁边的值是0.58%。

现在我尝试更改为:

<i class="<?php $file = file('include.txt');echo $file[1]; ?>"><strong> <?php $file = file('include.txt');echo $file[0]; ?></strong></i>

include.text的内容:第1行为0.58%,第2行为图标竖起大拇指。(我想每天在include.txt中更改为图标向上或向下,具体取决于第1行的值。)

如果file('include.txt')中的值是整数,则可以使用三元运算符来回显正确的css类,例如:

<li class="<?php echo (int) $file[0] < 0 ? 'icon-thumbs-down' : 'icon-thumbs-up'; ?>"></li>

这有点难以回复,因为您的代码不是那么清楚。然而,我可以通过一个例子给你以下建议:

$someInt = 10;
echo $someInt < 0 ? 'icon-negative' : 'icon-positive'; // will echo positive
$someInt = -3;
echo $someInt < 0 ? 'icon-negative' : 'icon-positive'; // will echo negative

这是一个短if/else(三元)。为了演示它是如何工作的,这在完整的语法中是相同的:

$someInt = -3;
if($someInt < 0){
    echo 'icon-negative';
}
else{
    echo  'icon-positive'; 
}

我会执行以下操作:

<?php
// your logic here, move in another file if this gets bigger:
function percentage($nb) {
  $file = file('include.txt');
  return $file[$nb];
}
?>
<i class="<?php echo percentage(5) > 0 ? 'positive' : 'negative' ?>">
  <strong>Text</strong>
</i>