简体   繁体   English

根据PHP变量值更改类

[英]Change class depending on PHP variable value

I am using PHP to retrieve data stored in a MySQL table and then display it on a page using the following the code: 我正在使用PHP检索存储在MySQL表中的数据,然后使用以下代码将其显示在页面上:

CSS 的CSS

.date {
color:red;
font-weight:bold;
}

HTML / PHP HTML / PHP

<span class="date">'.$date_available.'</span>

This works fine, but I need to assign a different class depending on the value within the $date_available variable. 这可以正常工作,但是我需要根据$ date_available变量中的值分配一个不同的类。 For example: if the variable contains the value SOLD, the span class should be as shown above. 例如:如果变量包含值SOLD,则跨度类应如上所示。 If the variable contains the value NOW, it should be: 如果变量包含值NOW,则应为:

HTML / PHP HTML / PHP

<span class="date-two">'.$date_available.'</span>

CSS 的CSS

.date-two {
color:green;
font-weight:bold;
}

Is this possible with PHP or will JavaScript need to be used? PHP可以做到这一点吗?还是需要使用JavaScript?

<?php
$cls = $data_available == "SOLD" ? "date-two" : "date";
echo "<span class=\"{$cls}\">{$date_available}</span>";

edit: explanation. 编辑:解释。 you check if your variable contains "SOLD", and based on that check save the name of the css-class to be used in a variable. 您检查变量是否包含“ SOLD”,并根据该检查保存要在变量中使用的css类的名称。 then, you don't output a fixed class-string, but the content of the variable. 那么,您不会输出固定的类字符串,而是输出变量的内容。

Depending on if you have one or more states you could go with either an if () {} else {} construct, use an array() for the job or a switch statement might also be usable if you have multiple states that require the same class to be output. 根据您是否具有一个或多个状态,可以使用if () {} else {}构造,对作业使用array() ,或者如果您有多个需要相同状态的状态,则switch语句也可能可用要输出的类。

for instance using an array: 例如使用数组:

$classes = array(
    'sold' => 'date-two',
    '...' => '...' //example values
);

// imagining that $data_available = 'SOLD'
echo '<span class="'. ($classes[strtolower($data_available)] || 'date') .'">';

or using a switch statement 或使用switch语句

$cls = 'date';
switch (strtolower($data_available)) {
    case 'sold':
    case 'sold2': // switch statements allow multiple 'cases' to be grouped for the same output.
        $cls = 'date-two';
    break;

    case '...':
        $cls = '...';
    break;

    default:
        $cls = 'date';
    break;
}

for the if example I'd look at Tobias' answer 对于if示例,我将看看Tobias的答案

Another option is instead of using a different class, simply know what the database can output and create prefixed classes for that. 另一个选择是不使用其他类,而只是知道数据库可以输出什么并为此创建前缀的类。

eg sold , bought and negotiating are some states (example) 例如, soldboughtnegotiating是某些州(示例)

What you could do here is simply 您在这里可以做的就是

$cls = 'date-' . strtolower($data_available); // if $data_available is sold it will output date-sold

echo '<span class=". $cls .">';

then in your CSS you could do something like: 然后在CSS中,您可以执行以下操作:

.date-sold {
    width: 420px;
    background: green;
}

.date-bought {
    width: 720px;
    background: white;
}

.date-negotiating {
    width: 30px;
    background: transparent;
}

this way you'll always be safe and if your database input is consistent which it should be then all your divs will get the right class with the right CSS applied without too much PHP hassle. 这样,您将永远是安全的,并且如果您的数据库输入是一致的,那么您的所有div都将获得正确的类,并且使用正确的CSS,而不会产生太多的PHP麻烦。

Good luck! 祝好运!

Yes its possible. 是的,它有可能。 You can use if to decide which class should be use for the span. 您可以使用if来决定该范围应使用哪个类。 You can easy add new classes with else if . 您可以轻松地通过else if添加新类。

if($date_available == "SOLD") {
    echo '<span class="date">'.$date_available.'</span>';
} else {
   echo '<span class="date-two">'.$date_available.'</span>';
}

Change the class on the way in: 在以下方式更改班级:

  <?  if ($date_available == 'whatever'){ ?>
       <span class="date"><? echo $date_available ?></span>
  <?  }else{ ?>
       <span class="date-two"><? echo $date_available ?></span>
  <?  } ?>
switch( $date_available ){
    case 'sold':$class='date-two';break;
    case 'available':$class='date';break;
    case 'low-stock':$class='date-three';break;
    /* etc */
}
echo "<span class='$class'>$date_available</span>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM