简体   繁体   中英

Change background color depend by value

I will try to best explain my problem and try to find solution. My table have cell called Status, i need to add code that swtich td class depend by value, if question is open add class open <td class='open'></td> , if quesiton is on hold add class onHold <td class='onHold'></td> ... Where from i get thet values? From database, so code look like this:

<tbody class="list-group-item-success"> // DEFAULT COLOR IS GREEN
    <tr>
        <td><?php echo $row['questionStatus'] ?></td>
    </tr>
</tbody>

Default color of cell's is GREEN because when question is posted, default is open, till administrator change to hold or closed or pending. So point is next add some code into that <td class=" HERE "></td> i think best is short javascript which will be defined by value of question status if closed add class green or add class red which i will define in css file. If u need more information about this, write me.

Thanks all

I think there is not need to create an if/else statements, if your possible values for

$row['questionStatus']

are:

'closed', 'open' and 'onHold' (any other word without spaces)

the only thing that you need to do is create css class with these names example:

.status-opened, .status-closed, .status-onHold

and then you need to implement it this way

<td class="status-<?php echo $row['questionStatus'] ?>">

so the final render when $row['questionStatus'] got resolve regarding the value will be :

<td class="status-opened">
<td class="status-closed">
<td class="status-onHold">

if you want to change the background without the need to reload the page, you will need to use JS stuff, I can give you an example as well.

You could use a switch-statement:

<?php
 switch($row['questionStatus']){
  case 'this': $class = 'this'; break;
  case 'that': $class = 'that'; break;
  default:     $class = 'dflt';
 }
?>

and output the $class-var within the class-attribute

<tbody class="something <?php echo $class ?>">

Don't forget to define the css-classes.

Open/Close simple example:

<?php

 $class = 'closed';
 if($row['questionStatus'] == 'open'){
  $class = 'open';
 }

The rest is the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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